Strings Methods & String Manipulation
len()
The len(object) function returns the number of characters in a string.
str = "Hello, world!"
length = len(str) # Output: 13
The length of a string can be determined using the [[len]] function.
{"cloze_text": "The length of a string can be determined using the [[len]] function."}
upper(), lower() and title()
Uppercase
The upper() method converts all characters in a string to uppercase. You can also use the lower() and title() methods.
str = "hello"
uppercaseStr = str.upper() # Output: "HELLO"
Lowercase
The lower() method converts all characters in a string to lowercase.
str = "hello"
lowercaseStr = str.lower() # Output: "hello"
Title case
The title() method capitalizes the first letter of each word in a string.
str = "hello"
titlecaseStr = str.title() # Output: "Hello"
The [[upper]] method converts all characters in a string to uppercase.
{"cloze_text": "The [[upper]] method converts all characters in a string to uppercase."}
replace()
The replace(original,replacement) method replaces a specified character or substring with another.
str = "Hello, world!"
newStr = str.replace("world", "John") # Output: "Hello, John!"
Note: the replace method does not alter the original string, rather it returns a new string
Which of the following statements is true about the 'replace' method?
{"answers": {"A": "It only works with strings of the same length", "B": "It can only replace a single character at a time", "C": "It modifies the original string", "D": "It returns a new string"}, "question_text": "Which of the following statements is true about the 'replace' method?", "correct_answer": "D"}
split()
The split(delimiter) method splits a string into an array of substrings based on a specified separator.
str = "Hello, world!"
parts = str.split(", ") # Output: ["Hello", "world!"]
These string methods provide powerful tools for working with text data and enable you to perform various manipulations and transformations on strings
The [[split]] method splits a string into a list of substrings based on a specified delimiter.
{"cloze_text": "The [[split]] method splits a string into a list of substrings based on a specified delimiter."}
Searching and Extracting Substrings
index()
The `index()` method returns the index of the first occurrence of a specified substring within a string.It can be used to determine whether a substring exists in a string and find its position if it does.
If the substring is not found, `index()` returns -1.
text = "Hello, world!"
index = text.index("world") # Output: 7
rfind()
Similar to `index()`, the `rfind()` method returns the index of the last occurrence of a specified substring within a string.
It can be used to find the last position of a substring within a string.
text = "Hello, world!"
last_index = text.rfind("o") # Output: 8
The [[index]] method returns the index of the first occurrence of a specified substring.
{"cloze_text": "The [[index]] method returns the index of the first occurrence of a specified substring."}
String slicing [stard:end]
You can extract a substring from a string using square brackets. The extracted substring includes the character at the starting index but excludes the character at the ending index.
text = "MOUSE" substring = text[1:5] # Output: "OUSE"
String slicing allows you to extract a [[substring]] from a larger string.
{"cloze_text": "String slicing allows you to extract a [[substring]] from a larger string."}
Negative Slicing
Negative slicing in Python allows you to specify indices relative to the end of the string.
text = "MOUSE"
substring = text[-4:-1] # Output: "OUS"
Negative indices can be used in string slicing to start counting from the [[end]] of the string.
{"cloze_text": "Negative indices can be used in string slicing to start counting from the [[end]] of the string."}
.join()
In Python, the join() method is used to concatenate elements of an iterable, such as a list, tuple, or string, into a single string.
my_list = ["apple", "banana", "cherry"]
result = ", ".join(my_list)
print(result) # Output: "apple, banana, cherry"
If the iterable contains non-string elements, you need to convert them to strings before using join().
numbers = [1, 2, 3, 4, 5]
result = "-".join(str(num) for num in numbers)
print(result) # Output: "1-2-3-4-5"
What is the purpose of the join() method in Python?
{"answers": {"A": "To concatenate strings together", "B": "To split a string into a list", "C": "To reverse a string", "D": "To remove whitespace from a string"}, "question_text": "What is the purpose of the join() method in Python?", "correct_answer": "A"}
Strings Methods & String Manipulation
- len()
- upper(), lower() and title()
- replace()
- split()
- Searching and Extracting Substrings
- String slicing [stard:end]
- Negative Slicing
- .join()
Topic Tests
Š learnlearn.uk 2025 |
[email protected] |
Privacy Policy.