7.4 String functions
String functions are essential in Python because they provide a way to manipulate and handle textual data efficiently. Strings are a core data type in Python, and these functions allow for various operations such as searching, modifying, and formatting text.
Here's an explanation of common string features and string functions that are required for this course.
7.4a String indexing (string[position]
) - get character from string
This feature retrieves a single character from a string at a specified position (index).
Explanation of string indexing: - String Indexing - allows you to retrieve a specific character from a string by specifying its position (index).
-
Zero-Based Indexing - Python uses zero-based indexing, meaning the first character of a string is at index 0, the second character is at index 1, and so on.
-
Negative Indexing - Python also supports negative indexing, where
-1
refers to the last character,-2
to the second last character, and so on.
Example 7.4.1 - String indexing (positive indexing)
Example 7.4.1 - Explanation
text[1]
accesses the character at index 1 of the string text
, which is 'e'
.
Example 7.4.2 - String indexing (negative indexing)
Example 7.4.2 - Explanationtext[-1]
retrieves the last character,'o'
.text[-2]
retrieves the second last character,'l'
.
7.4b Substring (string[start:end]
)
This operation extracts a part of the string starting from the start index up to but not including the end index.
Example 7.4.3 - with start:end
indices
This function extracts a part of the string, starting from the index 2 and up to (but not including) index 5.
text = "The quick brown fox jumps over the lazy dog"
start = 4
end = 9
substring = text[start:end] # 'quick'
Example 7.4.3 - Explanation
text[start:end]
slices the string from the index specified by start
to the index before end
. In this case, start
is 4
and end
is 9
, so the substring from index 4
to 8
is 'quick'
.
Example 7.4.4 with negative indices
Example 7.4.4 - Explanationtext[-6:]
slices the string from the 6th last character to the end of the string, resulting in 'Python'
.
7.4c String function - strip()
This function removes any leading (left) and trailing (right) whitespace characters from the string
Example 7.4.5
Example 7.4.5 - Explanation
text.strip()
removes the spaces before and after the word 'Hello'.
7.4d Function - len()
This function returns the length of the string, i.e., the number of characters it contains. len()
function in Python is not limited to strings; it works on various other data types as well. The len()
function returns the number of items in an object, and it can be used with any object that has a length, such as sequences and collections.
Example 7.4.6
Example 7.4.6 - Explanation
len(text)
returns 5
, as there are 5 characters in 'Hello'
.
7.4e String function - lower()
This function converts all characters in the string to lowercase.
Example 7.4.7
Example 7.4.7 - Explanation
text.lower()
changes 'Hello'
to 'hello'
.
7.4f String function - upper()
This function converts all characters in the string to uppercase.
Example 7.4.8
Example 7.4.8 - Explanation
text.upper()
changes 'Hello'
to 'HELLO'
.
7.4g String function - replace()
This function replaces all occurrences of a specified substring with another substring.
Example 7.4.9
Example 7.4.9 - Explanationtext.replace("World", "Python")
changes 'World'
to 'Python'
.
7.4h String function - find()
This function searches the string for a specified substring and returns the position (index) of where it was found. If the substring is not found, it returns -1.
Example 7.4.10
Example 7.4.10 - Explanation
text.find("World")
returns 6
, the starting index of 'World'
in the string.
7.4i Function - count()
This function returns the number of occurrences of a specified substring in the string. The count()
function in Python is versatile and works with both strings and collections such as lists, tuples, and other iterable types.
Example 7.4.11
Example 7.4.11 - Explanation
text.count("l")
returns 3
, as 'l'
appears three times in 'Hello World'
.
These functions are fundamental tools in Python for processing and analyzing textual data, making it easier to work with strings in various applications.
Exercise 7.4.1 - Given the string quote = "The quick brown fox jumps over the lazy dog"
, perform the following tasks:
- Extract the character at the 5th position in the string.
- Get the substring from the 10th to the 15th character.
- Remove any leading and trailing spaces from the string.
- Find the length of the string.
- Convert the entire string to lowercase.
- Convert the entire string to uppercase.
- Replace the word "lazy" with "energetic".
- Find the position of the word "fox" in the string.
- Count how many times the letter "o" appears in the string.
Exercise 7.4.1 - Model Answer - Make sure to work out the exercise before checking the model answer.