INTRODUCTION TO PYTHON STRINGS
INTRODUCTION
STRING in PYTHON are sequence of characters. Strings are enclosed by either single quotes, double quotes or triple quotes.
STRING DECLARATIONS
PYTHON Strings can be enclosed within:
- SINGLE QUOTES
- DOUBLE QUOTES
- TRIPLE QUOTES
# STRING DECLARATIONS example
print('Single Quotes - > Python is Excellent')
print("Double Quotes - > Python is Excellent")
print('''Triple Single Quotes - > Python is Excellent''')
print("""Triple Double Quotes - > Python is Excellent""")
INTRODUCTION TO PYTHON STRINGS : Output
Single Quotes - > Python is Excellent
Double Quotes - > Python is Excellent
Triple Single Quotes - > Python is Excellent
Triple Double Quotes - > Python is Excellent
STRING COMMENTS
Python strings are commented in multiple ways. For single line comment we will use # and then will put the comment.
# STRING COMMENTS example
print('SINGLE LINE COMMENT')
# SINGLE LINE COMMENT
print('MULTILINE COMMENT USED FOR SINGLE LINE')
'''MULTILINE COMMENT'''
print('MULTILINE COMMENT USED FOR MULTIPLE LINE')
'''LEARNING PYTHON
IS VERY INTERESTING
EVERYONE SHOULD LEARN PYTHON'''
INTRODUCTION TO PYTHON STRINGS : Output
SINGLE LINE COMMENT
MULTILINE COMMENT USED FOR SINGLE LINE
MULTILINE COMMENT USED FOR MULTIPLE LINE
For multiline comments we can use the # operator in each line like in below example.
# Multiple line comments example
str_var = "I'm learning Python nowadays!"
print("\nOriginal String -> ", str_var)
# Multiple line comment 1
# Multiple line comment 2
# Multiple line comment 3
print("\nAbove lines are for multiline comments but using single line comments")
INTRODUCTION TO PYTHON STRINGS : Output
Original String -> I'm learning Python nowadays!
Above lines are for multiline comments but using single line comments
But we can use triple single quotes for multiline comments as shown in below example.
# Multiple line comments example
str_var = "I'm learning Python nowadays!"
print("\nOriginal String -> ", str_var)
''' Multiple line comment 1
Multiple line comment 2
Multiple line comment 3'''
print("\nAbove lines are commented by multiline comments")
INTRODUCTION TO PYTHON STRINGS : Output
Original String -> I'm learning Python nowadays!
Above lines are commented by multiline comments
We can also use the triple double quotes for multiple line comments as shown below.
# Multiple line comments example
str_var = "I'm learning Python nowadays!"
print("\nOriginal String -> ", str_var)
""" Multiple line comment 1
Multiple line comment 2
Multiple line comment 3"""
print("\nAbove lines are commented by multiline comments")
INTRODUCTION TO PYTHON STRINGS : Output
Original String -> I'm learning Python nowadays!
Above lines are commented by multiline comments
PRINTING MULTILINE STRING
# PRINTING MULTILINE STRING example
print('''LEARNING PYTHON
IS VERY INTERESTING
EVERYONE SHOULD LEARN PYTHON''')
INTRODUCTION TO PYTHON STRINGS : Output
LEARNING PYTHON
IS VERY INTERESTING
EVERYONE SHOULD LEARN PYTHON
If a String contains any single quotes in it then to print the string we have to use double quotes inside print command.
# PRINTING STRINGS CONTAINING SINGLE QUOTES IN IT example
print("\nI'm learning Python..!!")
INTRODUCTION TO PYTHON STRINGS : Output
I'm learning Python..!!
If a String contains double quotes inside it then we will use single quotes inside print command.
# PRINTING STRINGS CONTAINING DOUBLE QUOTES IN IT example
print('\nHarry said "I am learning Python..!!"')
INTRODUCTION TO PYTHON STRINGS : Output
Harry said "I am learning Python..!!"
USING ESCAPE CHARACTER
ESCAPE characters help us to include single quote inside a string during printing when the entire string is enclosed with single quote. Same is true with double quotes as well. ESCAPE characters are identified by \ in Python.
# PRINTING STRINGS USING ESCAPE CHARACTERS example
print('\nHey I\'am learning Python..!!')
INTRODUCTION TO PYTHON STRINGS : Output
Hey I'am learning Python..!!
# PRINTING STRINGS USING ESCAPE CHARACTERS example
print('\nHey I\'am learning Python..!!')
print("\nHey I\'am learning Python..!!")
print("\nSayantan said \"I'am learning Python..!!\"")
INTRODUCTION TO PYTHON STRINGS : Output
Hey I'am learning Python..!!
Hey I'am learning Python..!!
Sayantan said "I'am learning Python..!!"
NEW LINE CHARACTER
New line characters help us to print a string in the next line. In Python new line characters are identified by \n.
# PRINTING STRINGS USING NEWLINE CHARACTERS example
print("\nHey how are you? \nI'am learning Python nowadays..!!")
INTRODUCTION TO PYTHON STRINGS : Output
Hey how are you?
I'am learning Python nowadays..!!
If we use multiple \n then multiline gap will be printed.
# PRINTING STRINGS USING NEWLINE CHARACTERS example
print("\nHey how are you? \n\nI'am learning Python nowadays..!!\n\n What about you??")
INTRODUCTION TO PYTHON STRINGS : Output
Hey how are you?
I'am learning Python nowadays..!!
What about you??
We can also use triple single quotes or triple double quotes to print string in multiple lines.
# PRINTING STRINGS IN MULTIPLE LINES USING TRIPLE SINGLE QUOTES example
print('''Hey how are you?
I'am learning Python nowadays..!!
What about you??''')
INTRODUCTION TO PYTHON STRINGS : Output
Hey how are you?
I'am learning Python nowadays..!!
What about you??
# PRINTING STRINGS IN MULTIPLE LINES USING TRIPLE DOUBLE QUOTES example
print("""Hey how are you?
I'am learning Python nowadays..!!
What about you??""")
INTRODUCTION TO PYTHON STRINGS : Output
Hey how are you?
I'am learning Python nowadays..!!
What about you??
Instead of using new line characters, it is advisable to use triple single quotes or triple double quotes during printing multiline strings.
If we try to print multiline string enclosed with one single quote or double quote the Python will raise error.
# PRINTING STRINGS IN MULTIPLE LINES USING SINGLE QUOTES example
print('Hey how are you?
I'am learning Python nowadays..!!
What about you??)
INTRODUCTION TO PYTHON STRINGS : Output
File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 7
print('Hey how are you?
^
SyntaxError: unterminated string literal (detected at line 7)
# PRINTING STRINGS IN MULTIPLE LINES USING DOUBLE QUOTES example
print('Hey how are you?
I'am learning Python nowadays..!!
What about you??)
INTRODUCTION TO PYTHON STRINGS : Output
File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 7
print('Hey how are you?
^
SyntaxError: unterminated string literal (detected at line 7)
HORIZONTAL TAB CHARACTER
Horizontal Tab Characters allow us to create a tab space horizontally.
# PRINTING STRINGS WITHOUT HORIZONTAL TAB CHARACTER example
print("\nHey how are you? I'am learning Python nowadays..!! What about you??")
# PRINTING STRINGS WITH HORIZONTAL TAB CHARACTER example
print("\nHey how are you? \tI'am learning Python nowadays..!! \tWhat about you??")
INTRODUCTION TO PYTHON STRINGS : Output
Hey how are you? I'am learning Python nowadays..!! What about you??
Hey how are you? I'am learning Python nowadays..!! What about you??
In the above example we can see the impact of using Horizontal tab character in the second print command.

RELATED TOPICS:
Pingback: PYTHON STRING MANIPULATION - Sayantan's Blog On Python Programming