12.1 List of Methods
In Python, the string data type comes with a rich set of methods that make working with strings convenient and powerful. Here are some of the most popular methods of the string
class:
Method | Description |
---|---|
strip() |
Removes whitespace from the beginning and end of the string. |
lower() |
Converts all characters in the string to lowercase. |
upper() |
Converts all characters in the string to uppercase. |
replace(old, new) |
Replaces all occurrences of the substring old with the substring new . |
split(separator) |
Splits the string into a list of substrings based on the given separator. |
join(iterable) |
Joins a collection of strings into one string using a separator string. |
find(substring) |
Returns the index of the first occurrence of the substring or -1 if the substring is not found.
|
index(substring) |
Returns the index of the first occurrence of the substring or raises an exception if not found. |
format() |
Formats the string, replacing braces with values. |
startswith(prefix) |
Checks if the string starts with the substring prefix . |
endswith(suffix) |
Checks if the string ends with the substring suffix . |
Note that a str
object cannot be changed after it's created. All functions that modify the string actually return a new object. The old object remains unchanged.
12.2 Most Popular Methods
Let's go through some of the simpler and more popular methods.
Method strip():
Removes whitespace from the beginning and end of the string.
text = " hello world! "
cleaned_text = text.strip()
print(cleaned_text) # Output: "hello world!"
Method lower()
:
Converts all characters in the string to lowercase.
text = "Hello World!"
lower_text = text.lower()
print(lower_text) # Output: "hello world!"
Method upper()
:
Converts all characters in the string to uppercase.
text = "Hello World!"
upper_text = text.upper()
print(upper_text) # Output: "HELLO WORLD!"
Method split(separator)
:
Splits the string into a list of substrings based on the given separator.
text = "one,two,three"
parts = text.split(',')
print(parts) # Output: ['one', 'two', 'three']
Method join(iterable)
:
Joins a collection of strings into one string using a separator string.
parts = ['one', 'two', 'three']
joined_text = ','.join(parts)
print(joined_text) # Output: "one,two,three"
Important!
Note that the join()
method is called on the separator string!
These methods are key tools for processing and manipulating text data in Python.
12.3 Searching and Replacing Substrings
There are a few other popular methods for searching and replacing strings within a string.
Method find(substring)
:
Returns the index of the first occurrence of the substring in the string or -1
if the substring is not found.
text = "hello world"
index = text.find("world")
print(index) # Output: 6
Method index(substring)
:
Similar to find
, but raises a ValueError
exception if the substring is not found.
text = "hello world"
try:
index = text.index("world")
print(index) # Output: 6
except ValueError:
print("Substring not found")
Method replace(old, new)
:
Replaces all occurrences of the substring old
with the substring new
.
text = "hello world"
replaced_text = text.replace("world", "everyone")
print(replaced_text) # Output: "hello everyone"
Method startswith(prefix)
:
Checks if the string starts with the specified prefix.
text = "hello world"
print(text.startswith("hello")) # Output: True
Method endswith(suffix)
:
Checks if the string ends with the specified suffix.
text = "hello world"
print(text.endswith("world")) # Output: True
These methods are very handy for various string searching, replacing, and checking operations, simplifying text data handling.
GO TO FULL VERSION