Advertisement



< Prev
Next >



Python - String






What is a String?


A string is nothing but a sequence of characters. To create a string object in Python, we can enclose a string literal i.e. a string of characters, within delimiters such as -



Creating string objects


A string object can be constructed by enclosing a string of characters within a pair of single quotes/ double quotes/ triple-single quotes/ triple-double quotes. Let us show you an example with how it is done.


Note : A string literal enclosed within a pair of single or double quotes can only span across a single line, whereas, a string literal enclosed within triple-single or triple-double quotes can span across multiple lines.


An example of creating string objects in different ways.
# Python - Example of string

# A string literal within a pair of single quotes
s1 = 'First'

# A string literal within a pair of double quotes
s2 = "Second"

# A string literal within a pair of triple-single quotes
s3 = '''Third'''

# A string literal within a pair of triple-double quotes
s4 = """Fourth"""


# A string literal within a pair of triple single quotes, spanning across multiple lines
s5 = '''Keep
Smiling!'''
print(s5)

# A string literal within a pair of triple double quotes, spanning across multiple lines
s6 = """Hello
    World!"""
print(s6)

# Printing the values of all string objects, one by one
print(s1)
print(s2)
print(s3)
print(s4)
print(s5)
print(s6)

Output


First
Second
Third
Fourth
Keep 
Smiling
Hello
    World!


Program Analysis


We have created 4 string objects in 4 different ways, where -

# Python - Example of string
 
 
# A string literal enclosed within a single quotes and
# It includes single quotes, preceding a backslash.
s1 = 'He\'s a good guy'


# A string literal enclosed within a double quotes and
# It includes double quotes, preceding a backslash.
s2 = "He said - \"I didn't do it\""


# A string literal enclosed within a pair of single quotes can
# include double/triple quotes without needing a backslash
s3 = '"Be yourself, everyone else is already taken - Oscar Widle"'


# A string literal enclosed within a pair of double quotes can
# include single/triple quotes withou needed a backslash
s4 = "It's a beautiful day today!"



# A string literal enclosed within a pair of triple-double quotes can
# include single quotes but a double quote in it must precede a backslash.
s5  = """\"Never say - I can't\""""


# Printing the string objects
print(s1)
print(s2)
print(s3)
print(s4)
print(s5)


Output is :


He's a good guy.
He said - "I didn't do it"
"Be yourself, everyone else is already taken - Oscar Widle"
It's a beautiful day today!
"Never say - I can't





Please share this article -





< Prev
Next >
< Python Dictionary
Python - String Methods >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement