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.

MethodSyntaxDescription
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.

MethodSyntaxDescription
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.