2.1. Strings in Python#
2.1.1. Understanding Strings as Sequences#
In Python, a string is fundamentally a sequence of characters. It’s a built-in data type designed for representing textual data and can enclose text within either single '
or double "
quotes. Strings in Python can range from a single character to lengthy paragraphs [Downey, 2015, Python Software Foundation, 2024].
2.1.2. Indexing in Python Strings#
Indexing allows access to each character in a string, forming the foundation for string manipulation. Python strings are zero-indexed, meaning the first character has an index of 0. For example, 'H'
is at index 0 in the string "Hello, Calgary!"
.
Table 2.1 illustrates how positive and negative indexing works in Python, using the string "Hello, Calgary!"
as an example:
Positive Index: Starts from 0 and increases by 1 for each subsequent character. For example,
H
is at index0
,e
at index1
, and so on.Negative Index: Starts from -1 for the last character and decreases by 1 for each preceding character. For example,
!
is at index-1
,y
at index-2
, and so on.
Positive Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Char |
H |
e |
l |
l |
o |
, |
C |
a |
l |
g |
a |
r |
y |
! |
|
Negative Index |
-15 |
-14 |
-13 |
-12 |
-11 |
-10 |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
This dual indexing system allows for flexible access to characters in a string, enabling both forward and backward traversal.
2.1.3. Strings are Immutable#
In Python, strings are immutable objects. This means that once a string is created, its contents cannot be altered. Any attempt to modify a string results in the creation of a new string object. This concept is essential for ensuring the integrity of string data. Let’s examine an example to illustrate the immutability of strings [Downey, 2015, Python Software Foundation, 2024]:
text = "Hello, Calgary!"
# Attempting to change a character at a specific index (this will raise an error)
text[0] = 'h' # Raises "TypeError: 'str' object does not support item assignment"
In this example, an attempt is made to change the first character of the string text
from ‘H’ to ‘h’. However, this operation raises a TypeError
because strings do not support item assignment.
The immutability of strings is a fundamental property in Python that ensures strings remain unchanged once created. This prevents unintended modifications and helps maintain the integrity of string data. To modify a string, one must use string methods or concatenation to create a new string with the desired changes, leaving the original string unchanged.
2.1.4. Index-Based Character Access#
In Python, string indexing allows you to access individual characters within a string. The indexing is zero-based, meaning the first character of the string has an index of 0, the second character has an index of 1, and so on. You can also use negative indexing, where -1 represents the last character, -2 represents the second-to-last character, and so on.
2.1.5. Examples of accessing characters in a string#
Example: Positive Indexing
Let’s consider the string "Hello, Calgary!"
. Using positive indexing:
Positive Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Char |
H |
e |
l |
l |
o |
, |
C |
a |
l |
g |
a |
r |
y |
! |
my_string[0]
accesses the first character: Hmy_string[7]
accesses the eighth character: C
my_string = "Hello Calgary!"
# Accessing individual characters using positive indexing
print(my_string[0]) # Output: "H"
print(my_string[7]) # Output: "C"
H
a
Example: Negative Indexing
Using negative indexing, the same string "Hello Calgary!"
can be visualized as:
Negative Index |
-15 |
-14 |
-13 |
-12 |
-11 |
-10 |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Char |
H |
e |
l |
l |
o |
, |
C |
a |
l |
g |
a |
r |
y |
! |
my_string[-1]
accesses the last character: !my_string[-8]
accesses the eighth character from the end: C
These indexing methods provide flexible ways to access and manipulate individual characters within a string.
# Accessing individual characters using negative indexing
print(my_string[-1]) # Output: "!"
print(my_string[-8]) # Output: "C"
!
C
2.1.6. Slicing for Substring Extraction#
In Python, string slicing allows you to extract a substring from a given string by specifying a range of indices. The general syntax for slicing a string is as follows:
string[start_index:stop_index]
Here’s what each part of the syntax means:
start_index
: The index of the first character of the substring (inclusive).stop_index
: The index of the first character after the end of the substring (exclusive).
The result of slicing will be a new string containing the characters from the start_index
up to, but not including, the stop_index
.
Examples of String Slicing
Let’s consider the string "Hello Calgary!"
and see various slicing examples:
text = "Hello Calgary!"
# Slice from index 0 to 5 (exclusive)
substring1 = text[0:5]
print(substring1) # Output: "Hello"
# Slice from index 7 to the end of the string
substring2 = text[7:]
print(substring2) # Output: "Calgary!"
# Slice from index 2 to 8 (exclusive)
substring3 = text[2:8]
print(substring3) # Output: "llo Ca"
# Slice from the beginning to index 5 (exclusive)
substring4 = text[:5]
print(substring4) # Output: "Hello"
# Slice the entire string (returns a copy of the original string)
substring5 = text[:]
print(substring5) # Output: "Hello Calgary!"
# Negative indices can also be used for slicing (counting from the end of the string)
substring6 = text[-7:-1]
print(substring6) # Output: "Calgary"
Hello
algary!
llo Ca
Hello
Hello Calgary!
algary
A visual representation of the string "Hello Calgary!"
with both positive and negative indices:
Positive Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Char |
H |
e |
l |
l |
o |
, |
C |
a |
l |
g |
a |
r |
y |
! |
|
Negative Index |
-15 |
-14 |
-13 |
-12 |
-11 |
-10 |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
text[0:5]
: Extracts characters from index 0 to 4, resulting in"Hello"
.text[7:]
: Extracts characters from index 7 to the end, resulting in"Calgary!"
.text[2:8]
: Extracts characters from index 2 to 7, resulting in"llo Ca"
.text[:5]
: Extracts characters from the beginning to index 4, resulting in"Hello"
.text[:]
: Extracts the entire string, resulting in a copy of"Hello Calgary!"
.text[-7:-1]
: Extracts characters from index -7 to -2, resulting in"Calgary"
.
2.1.7. Determining String Length with len()
#
In Python, the len()
function is used to find the length of a string or any other sequence (e.g., list, tuple). The len()
function returns the number of elements (characters in the case of a string) in the given sequence.
Here’s how you can use the len()
function to find the length of a string:
text = "Hello Calgary!"
length = len(text)
print(length) # Output: 14 (length of the string 'Hello Calgary!')
14
In this example, the len()
function is applied to the string variable text
, and it returns the length of the string, which is 14 characters, including spaces and punctuation.
The len()
function is a handy tool for performing various operations on strings, such as checking if a string is empty, iterating over characters, or controlling loops based on the length of a string. It is a simple and commonly used function in Python for working with sequences of data.
2.1.8. Iterating Over Strings with Loops#
You can iterate over the characters of a string in Python using various types of loops, such as for
loops and while
loops. Here’s an example using a for
loop to iterate over the characters of a string:
my_string = "Hello, Calgary!"
# Using a for loop to iterate over the characters of the string
for char in my_string:
print(char)
my_string = "Hello, Calgary!"
# Using a for loop to iterate over the characters of the string
for char in my_string:
print(char)
H
e
l
l
o
,
C
a
l
g
a
r
y
!
In this example, the for
loop iterates over each character in the my_string
variable, and the variable char
takes on the value of each character during each iteration of the loop.
If you want to access both the index and the character in the loop, you can use the enumerate()
function:
my_string = "Hello, Calgary!"
# Using a for loop with enumerate to get both index and character
for index, char in enumerate(my_string):
print(f"Index: {index},\tCharacter: {char}")
Index: 0, Character: H
Index: 1, Character: e
Index: 2, Character: l
Index: 3, Character: l
Index: 4, Character: o
Index: 5, Character: ,
Index: 6, Character:
Index: 7, Character: C
Index: 8, Character: a
Index: 9, Character: l
Index: 10, Character: g
Index: 11, Character: a
Index: 12, Character: r
Index: 13, Character: y
Index: 14, Character: !
In this example, the enumerate()
function is used to get both the index and the character in each iteration of the loop.
Note
In Python, the \t
character is used to represent a horizontal tab in strings, including in print statements. When you include \t
within a string and then print that string, it will insert a horizontal tab character at that position. Horizontal tabs are often used to create indentation in text.
2.1.9. String Repetition#
Repetition in Python refers to the process of creating a new string by repeating an existing string a certain number of times. You can achieve string repetition using the *
operator, which allows you to repeat a string by a specified integer factor. Here’s how it works:
original_string = "Hello, "
repeated_string = original_string * 3
print(repeated_string) # Output: "Hello, Hello, Hello, "
Hello, Hello, Hello,
In this example, the *
operator is used to repeat the original_string
three times, creating a new string repeated_string
that consists of the original string repeated three times.
This technique can be useful for creating repeated patterns, building strings with a specific number of characters, or any other scenario where string repetition is needed. Just remember to use an integer value as the right operand of the *
operator to specify how many times you want the string to be repeated.
The *
operator can also be used with other data types, such as lists and tuples, to achieve similar repetition behavior.
Examples of String Repetition:
Creating a Repeated Pattern:
pattern = "AB"
repeated_pattern = pattern * 5
print(repeated_pattern) # Output: "ABABABABAB"
ABABABABAB
Building a String with a Specific Number of Characters:
dash_line = "-" * 10
print(dash_line) # Output: "----------"
----------
Using Repetition in a More Complex String:
border = "=" * 20
header = f"{border}\nTitle\n{border}"
print(header)
====================
Title
====================
Note
The
*
operator for string repetition requires an integer value as the right operand.This operator can be used not only with strings but also with other sequences like lists and tuples.
Repetition is a handy tool for generating repeated patterns, filling strings with specific characters, and creating formatted outputs.
String repetition, combined with other string operations, provides powerful ways to manipulate and generate text in Python.
2.1.10. String Concatenation#
Concatenation is the process of merging two or more strings to create a new, unified string. In Python, strings are sequences of characters, and concatenation enables the blending of these character sequences, allowing the creation of longer and more comprehensive strings. There are several ways to concatenate strings in Python [Martelli et al., 2023, Python Software Foundation, 2024].
2.1.10.1. Using the +
Operator#
The +
operator is used to concatenate strings by placing them next to each other.
string1 = "Hello, "
string2 = "Calgary!"
result = string1 + string2
print(result) # Output: "Hello, Calgary!"
Hello, Calgary!
In this example, the +
operator combines the content of string1
and string2
to form a single string, which is then stored in the result
variable and printed.
2.1.10.2. Using .join()
#
The .join()
method combines elements of an iterable (e.g., list, tuple) using a specified separator.
words = ["The", "Lord", "of", "the", "Rings"]
combined_string = " ".join(words)
print(combined_string) # Output: "The Lord of the Rings"
The Lord of the Rings
In this example, the " ".join(words)
expression combines the elements of the words
list into a single string, separated by spaces.
Note
The
+
operator is straightforward for concatenating a few strings.The
.join()
method is more efficient for concatenating multiple strings, especially when they are stored in an iterable.Concatenation can be used to build complex strings from simpler components, making it a powerful tool for string manipulation in Python.