What Are String Methods?
You already know how to create strings and combine them. But strings can do so much more! Python gives you string methods — built-in tools that let you transform, search, and slice text.
To use a method, put a dot . after a string (or a variable holding a string), then the method name with parentheses:
HELLO
Changing Case
Use .upper() and .lower() to change all letters to uppercase or lowercase:
ALICE alice
Use .title() to capitalize the first letter of every word:
The Great Gatsby
Cleaning Up Strings
Use .strip() to remove extra spaces (or other characters) from the beginning and end:
hello
There's also .lstrip() (left only) and .rstrip() (right only).
Replacing Text
Use .replace(old, new) to swap parts of a string:
I like dogs
Splitting and Joining
.split() breaks a string into a list of words:
['one', 'two', 'three']
You can split on any character:
['red', 'green', 'blue']
.join() does the opposite — it glues a list into one string:
hello world
Searching in Strings
Use .find() to get the position of text inside a string. It returns -1 if not found:
6 -1
Use .count() to count how many times something appears:
3
Checking Start and End
.startswith() and .endswith() return True or False:
True False
The in Operator
Use in to check if a string contains another string:
True False
String Slicing
You can grab parts of a string using square brackets with [start:end]:
Pyt thon Pyth
Use [::-1] to reverse a string:
olleh
Chaining Methods
You can chain methods together — each one works on the result of the previous one:
hello-world