This document provides a comprehensive overview of Python’s built-in string methods, organized by category. Each method includes a brief description, a Python code example, and the corresponding output. Strings in Python are immutable, so these methods return new strings without modifying the original.
These methods modify the case of characters in a string.
Capitalizes the first character and converts the rest to lowercase.
text = "hello WORLD"
result = text.capitalize()
print(result)
Output: Hello world
Converts the string to lowercase, suitable for case-insensitive comparisons (more aggressive than lower()).
text = "ßtrAßE"
result = text.casefold()
print(result)
Output: strasse
Converts all characters to lowercase.
text = "Hello WORLD"
result = text.lower()
print(result)
Output: hello world
Converts all characters to uppercase.
text = "Hello World"
result = text.upper()
print(result)
Output: HELLO WORLD