Comprehensive Guide to Python Strings
Ohidur Rahman Bappy
MAR 22, 2025
Comprehensive Guide to Python Strings
Introduction
In Python, a string is an immutable sequence data type. It represents a sequence of Unicode characters, which can be enclosed in single, double, or triple quotes.
String Literals in Python
The following examples show valid string literals in Python:
'This is a string in Python' # Single quotes
"This is a string in Python" # Double quotes
'''This is a string in Python''' # Triple single quotes
"""This is a string in Python""" # Triple double quotes
Assigning Strings to Variables
String literals can be assigned to variables:
str1 = 'This is a string in Python'
print(str1)
str2 = "This is a string in Python"
print(str2)
Output:
This is a string in Python
This is a string in Python
Multi-line Strings
Multi-line strings must be enclosed in triple quotes:
str1 = '''This is
the first
Multi-line string.'''
print(str1)
str2 = """This is
the second
Multi-line
string."""
print(str2)
Output:
This is
the first
Multi-line string.
This is
the second
Multi-line
string.
Embedding Quotes in Strings
You can embed quotes within strings by using different enclosing quotes:
str1 = 'Welcome to "Python Tutorial" on TutorialsTeacher'
print(str1)
str2 = "Welcome to 'Python Tutorial' on TutorialsTeacher"
print(str2)
Output:
Welcome to "Python Tutorial" on TutorialsTeacher
Welcome to 'Python Tutorial' on TutorialsTeacher
String Length
Use the len()
function to find the length of a string:
greet = 'Hello'
print(len(greet)) # Output: 5
String Indexing
Strings are ordered collections of characters and can be indexed:
greet = 'hello'
print(greet[0]) # Output: 'h'
print(greet[1]) # Output: 'e'
print(greet[4]) # Output: 'o'
Negative indexing is also supported:
greet = 'hello'
print(greet[-1]) # Output: 'o'
Immutability of Strings
Strings in Python are immutable, meaning they cannot be modified.
greet = 'hello'
greet[0] = 'A' # This will throw an error
The 'str' Class
All strings are objects of the str
class:
greet = 'hello'
print(type(greet)) # Output: <class 'str'>
Use the str()
function to convert numbers to strings:
print(str(100)) # Output: '100'
print(str(True)) # Output: 'True'
Escape Sequences
Escape sequences allow special characters to be included in strings:
str1 = 'Welcome to \'Python Tutorial\' on TutorialsTeacher'
print(str1)
str2 = "Welcome to \"Python Tutorial\" on TutorialsTeacher"
print(str2)
Output:
Welcome to 'Python Tutorial' on TutorialsTeacher
Welcome to "Python Tutorial" on TutorialsTeacher
To ignore escape sequences, prefix the string with an r
or R
:
str1 = r'Welcome to \'Python Tutorial\' on TutorialsTeacher'
print(str1)
Common Escape Sequences
\\
Backslash\b
Backspace\n
Newline\t
Tab\xnn
Hexadecimal notation
Python String Operations
Python supports several operations on strings:
a = 'hello'
b = 'world'
# Concatenation
a_b = a + b # Output: 'helloworld'
# Repetition
a_3 = a * 3 # Output: 'hellohellohello'
# Indexing
t = a[2] # Output: 'l'
# Slicing
py = a[0:2] # Output: 'he'
# Membership
is_y_in_a = 'y' in a # Output: True
is_x_in_a = 'x' not in a # Output: True
Explore these string functionalities to master Python's handling of text data.