Python String Methods Cheat Sheet
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Common String Formatting Methods
Methods used to alter string casing, alignment, and spacing.
| Method | Syntax | Description |
|---|---|---|
| upper() | text.upper() | Converts all characters in the string to uppercase. |
| lower() | text.lower() | Converts all characters in the string to lowercase. |
| strip() | text.strip() | Removes leading and trailing whitespace. |
| replace() | text.replace(old, new) | Replaces all occurrences of a substring with another. |
String Search and Inspection
Methods to find indices, count occurrences, and inspect string prefix/suffix.
| Method | Syntax | Description |
|---|---|---|
| find() | text.find(sub) | Returns the lowest index of the substring if found, else -1. |
| count() | text.count(sub) | Returns the number of non-overlapping occurrences of a substring. |
| startswith() | text.startswith(prefix) | Returns True if the string starts with the specified prefix. |
| endswith() | text.endswith(suffix) | Returns True if the string ends with the specified suffix. |
Frequently Asked Questions
Do string methods modify the original string?
No, strings in Python are immutable. String methods always return a new string and leave the original unchanged.
What is the difference between find() and index()?
find() returns -1 if the substring is not found, whereas index() raises a ValueError exception.
Keep Learning
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Strings
Master string manipulation in Python. Learn string methods, slicing, concatenation, and formatting techniques with clear, executable code examples.
How to Reverse a String in Python
Learn how to reverse a string in Python using slicing, the reversed() function, and loop concatenation with visual code examples.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.