Python Assignment - Print and String in Python

I ran a Python script using the print () function and string variables.


Code:

# Example 1: Basic print statement

print('Hello, world!')


# Example 2: Creating a string using single quotes

String1 = 'Welcome to the Geeks World'

print("String with the use of Single Quotes:")

print(String1)


Output:

Hello, world!

String with the use of Single Quotes:

Welcome to the Geeks World


Explanation:

The print () function is used to display output to the screen. In this case, it printed both a simple message (Hello, world!) and a string stored in a variable.

Strings in Python are sequences of characters enclosed in either single quotes (') or double quotes ("). You can use either style, but they must match (open and close the same way).

In this example:

  • I created a string called String1 using single quotes.

  • Then I used print() to output both a label and the string itself.

Together, print() and string variables work perfectly because print() can accept both string literals and string variables as input.


References:

Python 2 String Docs:

https://docs.python.org/2/library/string.html

Python 3 Print Docs:

https://docs.python.org/3/library/functions.html#print

Comments

Popular posts from this blog

Python Assignment – Intro to Functions and Lists

Python Assignment - Roots & Reciprocals

Using a Rectangle Class in Python