← All references
String Methods
str.upper() / str.lower() / str.strip() / str.split() / str.replace()Common operations on strings.
String Methods
Strings in Python are immutable — methods return new strings.
| Method | Effect |
|---|---|
.upper() | Uppercase |
.lower() | Lowercase |
.strip() | Remove leading/trailing whitespace |
.split(sep) | Split into list |
.replace(a, b) | Replace all a with b |
.startswith(p) | True if starts with p |
Example
s = " Hello, World! "\nprint(s.strip()) # "Hello, World!"\nprint(s.strip().upper()) # "HELLO, WORLD!"\nprint(s.strip().split(",")) # ["Hello", " World!"]\nprint(s.replace("World", "Python"))