Python String
A Python string is a sequence of characters and a character is simply a symbol.
Computers internally handle strings through sequences of bits.
To convert the bits into characters that people can understand, a conversion process is carried out using a certain encoding.
The most popular standard encodings are ASCII and Unicode.
In Python, a string is a sequence of Unicode characters.
You can learn about the history of Unicode from official documentation.
Create Python String
Strings can be created by enclosing characters within single or double quotation marks.
Triple quotes can also be used in Python, but they are typically used to represent multiline strings and docstrings.
# all of the following are equivalent
my_string = 'Hello'
print(my_string)
my_string = "Hello"
print(my_string)
my_string = '''Hello'''
print(my_string)
# Triple-quoted strings can span multiple lines
my_string = """Hello, this is
a multi-line string"""
print(my_string)
Access characters in a string
Individual characters can be accessed using indexing and ranges of characters using slicing.
with Index
- In Python, the index starts from 0.
- The index must be an integer. Using values of any other type will result in a
TypeError
.
If you attempt to access elements outside the index bounds, an IndexError
is generated.
s = 'Tutorial Reference'
print('s = ', s) # s = Tutorial Reference
print('s[0] = ', s[0]) # s[0] = T
print('s[1] = ', s[1]) # s[1] = u
print('s[5] = ', s[5]) # s[5] = i
# IndexError: string index out of range
# print(s[15])
# TypeError: string indices must be integers
# print(s[1.2])
with Negative Index
Python allows negative indexing for its sequences.
s = 'Tutorial Reference'
print('s = ', s) # s = Tutorial Reference
print('s[-1] = ', s[-1]) # s[-1] = e
print('s[-2] = ', s[-2]) # s[-2] = c
print('s[-5] = ', s[-5]) # s[-5] = r
Slicing
A range of elements in a string can be accessed using the slicing operator :
.
The possible cases (with examples) are shown in the table below:
Slice | Description | Example with s="abcd" |
---|---|---|
my_str[from:to] | the elements from from to to-1 | s[1:3] -> "bc" |
my_str[from:] | the elements from from to the end of the string | s[2:] -> "cd" |
my_str[:to] | the elements from the beginning of the list to the element to-1 | li[:3] -> "abc" |
my_str[:] | all elements | s[:] -> "abcd" |
my_str[from:to:step] | one element each step from from to to-1 | s[0:3:2] -> "ac" |
my_str[from::step] | one item each step from from until the end of the string | s[1::2] -> "bd" |
my_str[:to:step] | one element each step from the beginning to the to-1 element. | s[:3:2] -> "ac" |
my_str[::step] | one item each step | s[::2] -> "ac" |
Python String Manipulation and Operations
Strings are immutable, i.e., elements of a string can't be changed once they have been assigned.
Change a string
You can simply reassign different strings to the same name.
my_str = 'Tutorial Referece'
# my_str[3] = 'a'
# TypeError: 'str' object does not support item assignment
my_str = 'Other String'
print(my_str) # my_str
Delete a string
Characters can't be deleted or removed from a string because strings are immutable.
However, it is possible to completely delete a string using the del
statement
my_str = "a string"
print(my_str) # a string
# del my_str[1]
# TypeError: 'str' object doesn't support item deletion
del my_str # delete my_str
# print(my_str)
# NameError: name 'my_str' is not defined
Concatenation of Two or More Strings
Concatenation is the joining of two or more strings into one string
- In Python the
+
operator performs concatenation. - The
*
operator can be used to repeat the string a specified number of times.
str1 = 'Hello'
str2 ='World'
# Concatenation using + operator
print(str1 + str2) # HelloWorld
# Repetition using * operator
print(str1 * 3) # HelloHelloHello
Also, writing two string literals together concatenates them, as does the +
operator.
If you want to concatenate strings in different rows, you can use parentheses.
# two string literals together
my_str = 'Tutorial ''Reference'
print(my_str) # Tutorial Reference
# using parentheses
s = ('Tutorial '
'Reference')
print(s) # Tutorial Reference
Iterating Through a string
Using a for
loop, it is possible to iterate each character of a string.
count = 0
for letter in 'Tutorial Reference':
if(letter == 'e'):
count += 1
print(count,'letters found')
Output
4 letters found
String Membership Test
The keyword in
is used to check whether a character exists in a string.
my_str= 'Tutorial Reference'
print('t' in my_tuple) # True
print('h' in my_tuple) # False
print('q' not in my_tuple) # True
Built-in functions to Work with Python
Some of the commonly used built-in functions are enumerate()
and len()
.
- The
enumerate()
function returns an enumerate object. It contains the index and value of all the elements of the string as pairs. This can be useful for iteration. len()
function returns the length (i.e., the number of characters) of the string.
my_str = 'Tutorial'
# enumerate()
list_enumerate = list(enumerate(my_str))
print('list(enumerate(my_str) = ', list_enumerate)
#character count
print('len(my_str) = ', len(my_str ))
Output
list(enumerate(my_str) = [(0, 'T'), (1, 'u'), (2, 't'), (3, 'o'), (4, 'r'), (5, 'i'), (6, 'a'), (7, 'l')]
len(my_str) = 8
Learn more in Python String Methods reference.
Python String Formatting
Escape Sequence
Consider this string: He said, "What's there?"
. This will result in a SyntaxError
as the text itself contains both
single and double quotes.
One way around this problem is to use triple quotes. Alternatively, escape sequences can be used.
An escape sequence begins with a backslash and it is interpreted.
If a single quote is used to represent a string, all single quotes within the string must be escaped.
Similarly, if a double quote is used to represent a string, all double quotes within the string must be escaped.
# using triple quotes
print('''He said, "What's there?"''')
# escaping single quotes
print('He said, "What\'s there?"')
# escaping double quotes
print("He said, \"What's there?\"")
Output:
He said, "What's there?"
He said, "What's there?"
He said, "What's there?"
Here is a list of all the escape sequences supported by Python.
Escape Sequence | Description |
---|---|
\newline | Backslash and newline ignored |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\a | ASCII Bell |
\b | ASCII Backspace |
\f | ASCII Formfeed |
\n | ASCII Linefeed |
\r | ASCII Carriage Return |
\t | ASCII Horizontal Tab |
\v | ASCII Vertical Tab |
\ooo | Character with octal value ooo |
\xHH | Character with hexadecimal value HH |
For example:
print("C:\\Python32\\Lib")
print("This is printed\nin two lines")
print("This is \x48\x45\x58 representation")
# C:\Python32\Lib
This is printed
in two lines
This is HEX representation
Raw String to ignore escape sequence
To ignore escape sequences within a string, r
or R
must be placed in front of the string.
This implies that it is a raw string and any escape sequence within it will be ignored.
For example:
print("This is \x61 \ngood example")
print(r"This is \x61 \ngood example")
Output
This is a
good example
This is \x61 \ngood example
The format()
Method for Formatting Strings
The format()
method is used for formatting strings.
Format strings contain curly braces {}
as placeholders or replacement fields which get replaced.
Positional arguments or keyword arguments can be used to specify the order in the format string.
# default(implicit) order
default_order = "{}, {} and {}".format('Tom','Ryan','John')
print(default_order) # Tom, Ryan and John
# order using positional argument
positional_order = "{1}, {0} and {2}".format('Tom','Ryan','John')
print(positional_order) # Ryan, Tom and John
# order using keyword argument
keyword_order = "{t}, {r} and {j}".format(t='Tom',r='Ryan',j='John')
print(keyword_order) # Tom, Ryan and John
Old style formatting wit %
operator
Using the %
operator, strings can be formatted (like sprintf()
in C language).
x = 123.456789
print('x = %3.2f' %x) # x = 123.46
print('x = %3.4f' %x) # x = 123.4568
The format()
method has optional format specifications. For example is possible to left-justify <
, right-justify >
or center ^
a string in the given space.
It is suggested to use the more recent f-string interpolation or see official documentation to learn more.
Other Common String Operations
There are numerous methods available with the string object.
Some of the commonly used methods are format()
(discussed
above), lower()
, upper()
, join()
, split()
, find()
, replace()
etc.
my_str = "TuTOriAl RefERenCe"
print(my_str.lower())
print(my_str.upper())
lst = "This will split all words into a list".split()
print(lst)
my_str = ' '.join(['This', 'will', 'join', 'all', 'words', 'into', 'a', 'string'])
print(my_str)
'This will join all words into a string'
index = 'Happy Birthday'.find('py')
print(index)
my_str = 'Happy Birthday'.replace('Birthday','Christmas')
print(my_str)
Output
tutorial reference
TUTORIAL REFERENCE
['This', 'will', 'split', 'all', 'words', 'into', 'a', 'list']
This will join all words into a string
3
Happy Christmas