Python Declare, assign and print the string (Different ways)

Python Declare, assign and print the string (Different ways)

Here, we will figure out how to proclaim, appoint and print the string utilizing various routes in Python?

Proclaim string object, allot string (utilizing various ways), and print the string in Python.

These are a portion of the approaches to allot strings to the string object.

Ways Syntax Description
Way1Single quotes
Message
Assign a single line string.
Way2 Double quotes
Message
Assign a single line string.
Way3 Triple single quotes
”’Message”’
Assign a single line as well as a multi-line string.
Way4 Triple double quotes
“””Message“””
Assign a single line as well as a multi-line string.

Program:

# Declare, assign string (1)
# using single quotes 'string'
str1 = 'Hello world, How are you?'

# Declare, assign string (2)
# using double quotes "string"
str2 = "Hello world, How are you?"

# Declare assign string (3)
# using triple single quotes '''string'''
str3 = '''Hello world, How are you?'''

# Declare assign string (4)
# using triple double quotes """string"""
str4 = """Hello world, How are you?"""

# Declare, assign multi-line string (5)
# Triple double quotes allows to assign
# multi-line string
str5 = '''Hello world,
How are you?'''

# print the string
print "str1: ", str1
print "str2: ", str2
print "str3: ", str3 
print "str4: ", str4
print "str5: ", str5

Output:

    str1:  Hello world, How are you?
    str2:  Hello world, How are you?
    str3:  Hello world, How are you?
    str4:  Hello world, How are you?
    str5:  Hello world,
    How are you?

Recent Posts

error: Alert: Content is protected!!