lower()
Converts all characters in a string to lowercase.
python
Copy code
text = "Hello, World!"
result = text.lower()
print(result) # Output: "hello, world!"
2. upper()
Converts all characters in a string to uppercase.
python
Copy code
text = "Hello, World!"
result = text.upper()
print(result) # Output: "HELLO, WORLD!"
3. startswith(prefix)
Checks if the string starts with the specified prefix. Returns True or False.
python
Copy code
text = "Hello, World!"
print(text.startswith("Hello")) # Output: True
print(text.startswith("World")) # Output: False
4. endswith(suffix)
Checks if the string ends with the specified suffix. Returns True or False.
python
Copy code
text = "Hello, World!"
print(text.endswith("World!")) # Output: True
print(text.endswith("Hello")) # Output: False
5. split(separator)
Splits a string into a list of substrings based on the specified separator.
python
Copy code
text = "apple,banana,orange"
result = text.split(",")
print(result) # Output: ['apple', 'banana', 'orange']
If no separator is specified, it splits by whitespace:
python
Copy code
text = "Hello World Python"
result = text.split()
print(result) # Output: ['Hello', 'World', 'Python']
6. rstrip()
Removes any trailing whitespace or specified characters from the right side of a
string.
python
Copy code
text = "Hello, World! "
result = text.rstrip()
print(result) # Output: "Hello, World!"
7. index(substring)
Finds the first occurrence of the substring and returns its index. Raises a
ValueError if the substring is not found.
python
Copy code
text = "Hello, World!"
result = text.index("World")
print(result) # Output: 7
8. find(substring)
Finds the first occurrence of the substring and returns its index. Returns -1 if
the substring is not found.
python
Copy code
text = "Hello, World!"
result = text.find("World")
print(result) # Output: 7
print(text.find("Python")) # Output: -1
9. removesuffix(suffix) (Available from Python 3.9)
Removes the specified suffix from the end of the string. If the string does not end
with the suffix, the original string is returned.
python
Copy code
text = "document.txt"
result = text.removesuffix(".txt")
print(result) # Output: "document"
10. removeprefix(prefix) (Available from Python 3.9)
Removes the specified prefix from the beginning of the string. If the string does
not start with the prefix, the original string is returned.
python
Copy code
text = "prefix_value"
result = text.removeprefix("prefix_")
print(result) # Output: "value"
11. lowercase
While there's no direct method named lowercase, lower() is used for converting to
lowercase (covered earlier).
12. uppercase
Similarly, upper() is used to convert strings to uppercase (covered earlier).
Additional Notes
startswith() and endswith() can also accept tuples for checking multiple prefixes
or suffixes.
rstrip() can remove specific characters by passing them as an argument:
python
Copy code
text = "hello!!!"
print(text.rstrip("!")) # Output: "hello"
ChatGPT can make mistakes. Check important info.
?