10. String manipulations#
We will soon be creating a program designed to handle data. The next parts of the course will therefore focus on presenting the elements of Python that we are missing, in particular error handling, and file manipulation. Before that, we will deepen our understanding of strings.
10.1. Special Characters#
Special characters are used to control formatting within strings. These include:
Newline (
\n
): Moves the cursor to the beginning of the next line, which is useful for formatting multi-line text.Tab (
\t
): Inserts a horizontal tab, which can be used to align text.Backslash (
\\
): Inserts a literal backslash, which is necessary because a single backslash is used as an escape character.Single Quote (‘): Inserts a single quote inside a string enclosed by single quotes.
Double Quote (“): Inserts a double quote inside a string enclosed by double quotes.
Raw strings are prefixed with an r
and treat backslashes as literal characters rather than escape characters. This feature is particularly useful for file paths where backslashes are common.
10.2. Changing case#
Python provides methods for changing case of strings. Their usage is similar to list methods, using dot notation.
>>> "hello".uppper()
'HELLO'
>>> "HELLO".lower()
'hello'
"hello".capitalize()
'Hello'
10.3. Testing the characters#
Python provdies functions to determine what kind of characters are composing a string.
>>> "Hello".isalpha()
True
>>> "Hello!".isalpha()
False
>>> "12".isnumeric()
True
>>> "12.2".isnumeric()
False
>>> "\t".isspace()
True
>>> " ".isspace()
True
>>> ":".isspace()
False
>>> "Hello".islower(), "Hello".isupper()
(False, False)
>>> "hello".islower(), "hello".isupper()
(True, False)
10.4. String Methods for Cleaning, Splitting#
The strip
function Removes whitespace from both ends of the string. The functions lstrip
and rstrip
are similar but limited on a side.
text = " hello "
print(text.strip()) # Output: hello
The function split
splits the string into a list based on a specified delimiter.
text = "apple,banana,cherry"
print(text.split(',')) # Output: ['apple', 'banana', 'cherry']
The reverse function is join
.
",".join(["a", "b", "c"]) # Output: "a,b,c"
10.5. String Search Functions#
Python offers several methods to search and locate substrings.
The in
operator checks if a substring is present in the string.
text = "Hello, World!"
print("World" in text) # Output: True
The find
functions returns the index of the first occurrence of a substring, or -1 if not found. The index
function is similar to find
, but raises a ValueError
if the substring is not found.
text = "Hello, World!"
print(text.find("World")) # Output: 7
The rfind
is similar but returns the index of the last occurrence of a substring, or -1 if not found.
text = "Hello, World! World!"
print(text.rfind("World")) # Output: 13
Finaly, the count
function determine the number of occurrences of a substring.
text = "Hello, World! World!"
print(text.count("World")) # Output: 2
10.6. String Formatting#
Python offers several methods to format strings, allowing for dynamic content insertion.
The format
method replaces {}
placeholders with values provided as arguments.
name = "Alice"
age = 30
formatted_string = "Name: {}, Age: {}".format(name, age)
print(formatted_string) # Output: Name: Alice, Age: 30
You can specify positions or use keywords for greater clarity.
formatted_string = "Name: {0}, Age: {1}".format(name, age)
formatted_string_kw = "Name: {name}, Age: {age}".format(name="Alice", age=30)
print(formatted_string) # Output: Name: Alice, Age: 30
print(formatted_string_kw) # Output: Name: Alice, Age: 30
Placeholders can be reused within the same format string.
formatted_string = "The {0} is {1}. Again, the {0} is {1}.".format('color', 'blue')
print(formatted_string) # Output: The color is blue. Again, the color is blue.
You can format numbers to a specific precision.
pi = 3.14159
formatted_string = "The value of pi is {:.2f}".format(pi)
print(formatted_string) # Output: The value of pi is 3.14
The f-Strings (Formatted String Literals) provide a concise and readable way to format strings by embedding expressions directly.
name = "Alice"
age = 30
f_string = f"Name: {name}, Age: {age}"
print(f_string) # Output: Name: Alice, Age: 30
You can include expressions and perform calculations within f-strings.
a = 10
b = 20
f_string = f"The sum of {a} and {b} is {a + b}"
print(f_string) # Output: The sum of 10 and 20 is 30
Similar to format(), f-strings can format numbers.
pi = 3.14159
f_string = f"The value of pi to 2 decimal places is {pi:.2f}"
print(f_string) # Output: The value of pi to 2 decimal places is 3.14
F-strings also allow for padding and alignment.
f_string = f"|{'left':<10}|{'center':^10}|{'right':>10}|"
print(f_string) # Output: |left | center | right|
To include literal braces in the output, double them.
value = 42
f_string = f"{{value}} is not the same as {value}"
print(f_string) # Output: {value} is not the same as 42