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
In Python, which statement about the string 'replace' method is true?
{"answers": {"A": "It can only replace substrings of the same length", "B": "It only replaces the first occurrence of the specified substring", "C": "It returns a new string with replacements made", "D": "It modifies the original string in place"}, "question_text": "In Python, which statement about the string 'replace' method is true?", "correct_answer": "C"}
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 does the join() method do in Python?
{"answers": {"A": "Removes whitespace from the beginning and end of a string", "B": "Splits a string into a list based on a delimiter", "C": "Reverses the characters in a string", "D": "Concatenates elements of an iterable into a single string using a specified separator"}, "question_text": "What does the join() method do in Python?", "correct_answer": "D"}
String formatting tutorial
String methods in Python
Review: Fill in the Blanks
The function returns the number of in a string. For example, using len on the string "Hello, world!" produces a length of .
The method converts all characters in a string to . Similarly, the method changes all characters to , and the method capitalizes the first letter of each in a string.
The method is used to substitute a specified character or substring with another, creating a new string while leaving the original string . The method divides a string into an array of substrings based on a specified .
When extracting a substring, Python uses along with start and end indices, including the character at the starting index but excluding the character at the ending index. Negative slicing allows specifying indices relative to the of the string. The method concatenates elements of an iterable into a single string, but non-string elements must first be converted to .
The method converts all characters in a string to . Similarly, the method changes all characters to , and the method capitalizes the first letter of each in a string.
The method is used to substitute a specified character or substring with another, creating a new string while leaving the original string . The method divides a string into an array of substrings based on a specified .
When extracting a substring, Python uses along with start and end indices, including the character at the starting index but excluding the character at the ending index. Negative slicing allows specifying indices relative to the of the string. The method concatenates elements of an iterable into a single string, but non-string elements must first be converted to .
Click the keywords below to fill in the blanks:
Characters
Len()
Uppercase
Square Brackets
Separator
Title()
Join()
Word
Strings
Replace()
Upper()
End
Unaltered
Lower()
13
Lowercase
Split()
â All blanks filled correctly! Great job!
Complete! Ready to test your knowledge?
Strings Methods & String Manipulation
- len()
- upper(), lower() and title()
- replace()
- split()
- Searching and Extracting Substrings
- String slicing [stard:end]
- Negative Slicing
- .join()
Topic Tests
Videos
- String formatting tutorial
- String methods in Python