Python's string functions are built-in methods and operations that let you inspect, transform, split, join and format text without importing anything. The complete list lives on every string object: you can see it by running print(dir("")) in a Python 3 interpreter, or by checking the official documentation for str methods. This guide walks you through finding, testing and using them safely, with the exact method names you can rely on.
What You'll Need
- A working Python 3 installation (any recent 3.x version). Check with
python --versionorpython3 --versionin your terminal. - A way to run code: the interactive REPL, IDLE, VS Code, PyCharm, or any editor that can run a .py file.
- No third-party packages. All string methods are part of the standard library.
Step-by-Step
- Open a Python session. In a terminal, type
python3(orpythonon Windows) and press Enter. You should see the>>>prompt. - List every string method. Run
print([m for m in dir("") if not m.startswith("_")]). This prints the public method names only, filtering out dunder methods. The output is the practical complete list for your Python version. - Read the built-in help. For any method, run
help(str.split)orhelp("".split). The docstring gives the signature, arguments and a short example. This is more reliable than memorising a list because it matches your installed version. - Group the methods by purpose. Work through them in categories so the list becomes usable:
- Case:
lower(),upper(),casefold(),capitalize(),title(),swapcase(). - Search and test:
find(),rfind(),index(),rindex(),count(),startswith(),endswith(),in(operator, not a method). - Trim and pad:
strip(),lstrip(),rstrip(),ljust(),rjust(),center(),zfill(). - Split and join:
split(),rsplit(),splitlines(),partition(),rpartition(),join(). - Replace and translate:
replace(),translate(),maketrans(),expandtabs(). - Classify characters:
isalpha(),isdigit(),isalnum(),isspace(),isupper(),islower(),istitle(),isnumeric(),isdecimal(),isidentifier(),isprintable(),isascii(). - Format:
format(),format_map(), plus f-strings (syntax, not a method). - Encode:
encode()returns bytes; the reverse isbytes.decode().
- Case:
- Test each method on a sample string. In the REPL, assign
s = " Hello, World "and trys.strip(),s.lower(),s.split(","),s.replace("World", "Python"). Notice that methods return a new string; they do not changes. - Confirm immutability. Run
s[0] = "H". Python raisesTypeError: 'str' object does not support item assignment. To change a string, build a new one with slicing,replace()orjoin(). - Check the official list for your version. Open the Python documentation page for "Built-in Types" and scroll to "String Methods". This is the authoritative complete list; it is updated with each release. Bookmark it rather than copying a static list.
- Write a small cheat sheet. Save a .py file with one example per category from step 4. Running it once is the fastest way to remember which method does what.
Common Mistakes
- Expecting in-place changes.
s.upper()returns a new string. If you writes.upper()without assigning the result, nothing appears to happen. - Confusing
find()andindex(). Both return a position, butfind()returns-1when missing whileindex()raisesValueError. - Using
strip()to remove internal characters.strip()only trims from the ends; usereplace()for characters in the middle. - Calling
join()on the list. The separator is the string:", ".join(items), notitems.join(", "). - Assuming
isdigit()covers all numbers. It returns True for digit characters but not for decimals or negative signs. Use a try/except withfloat()when validating numbers. - Mixing bytes and str.
b"abc".upper()works, but you cannot concatenate bytes with str; decode first.
Tips
- Use
casefold()instead oflower()for case-insensitive comparison of non-English text. - Prefer f-strings for formatting; they are shorter and faster than
format()in most cases. - Chain methods left to right:
s.strip().lower().replace(" ", "_"). - Use
str.maketrans()withtranslate()for multiple character replacements in one pass. - When splitting on whitespace, call
split()with no argument; it collapses runs of spaces and ignores leading/trailing whitespace.
Troubleshooting
- AttributeError: 'str' object has no attribute ... Check spelling and Python version. Some methods are newer; run
python --versionand compare with the docs. - TypeError from join(). Every item in the iterable must be a string. Convert with
str(x)or a generator:", ".join(str(x) for x in items). - Unexpected result from split(). With no argument it splits on any whitespace; with a separator it keeps empty strings. Test both forms on your data.
- Encoding errors. Specify the encoding explicitly:
open("file.txt", encoding="utf-8"). Do not rely on the platform default. - Method seems missing. Confirm you are on a str, not a list or bytes. Run
type(your_variable).
Frequently Asked Questions
How many string methods does Python have?
The count changes between versions as methods are added. Run print(len([m for m in dir("") if not m.startswith("_")])) to get the exact number for your interpreter.
Are string methods in-place?
No. Strings are immutable, so every method returns a new string. Assign the result to a variable.
What is the difference between strip, lstrip and rstrip?
strip() removes characters from both ends, lstrip() only from the left, and rstrip() only from the right. By default they remove whitespace.
Can I use string methods on bytes?
Bytes objects have a similar but smaller set of methods. Decode to str first if you need text operations like casefold() or isalpha().
Where is the official complete list?
In the Python documentation under "Built-in Types", section "String Methods". It is the authoritative reference for your version.
Do string methods modify the original variable?
No. They return a new value. Reassign if you want to keep the change.