Advertisement
# 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)
First
Second
Third
Fourth
Keep
Smiling
Hello
World!
Advertisement
# Enclosing a string literal within a pair of mismatching delimiters
s1 = 'Hello"
# Python - Example of multi-word string
# A multi-word string literal within a pair of single quotes
s1 = 'Hello World!'
# A multi-word string literal within a pair of double quotes
s2 = "Welcome to DecodeJava!"
# A multi-word string literal within a pair of triple-single quotes
s3 = '''May you all have a wonderful life.'''
# A multi-word string literal within a pair of triple-double quotes
s4 = """Best Wishes!"""
# Printing the values of all string objects, one by one
print(s1)
print(s2)
print(s3)
print(s4)
Hello World!
Welcome to DecodeJava!
May you all have a wonderful life.
Best Wishes!
# Python - String objects can be added by using the + operator
# Creating the first string object
s1 = 'New '
# Creating the second string object
s2 = 'York'
# Creating the third string object
s3 = 'Delhi'
# Creating the fourth string object
s4 = 'Castle'
# Creating the fifth string object by concatenating two string literals
s5 = 'Never ' + 'Give ' + 'Up!'
# Addition of two string objects by + operator
print(s1 + s2)
print(s1 + s3)
print(s1 + s4)
print(s5)
NewYork
NewDelhi
NewCastle
Never Give Up!
# 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)
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement