Python String Methods

Python String Methods

kishore V


Python String Methods

Python strings come with a rich collection of built-in methods that allow you to analyze, modify, format, and validate text efficiently.

Important Note:

Strings in Python are immutable. This means every string method returns a new string and never alters the original one.

Commonly Used Python String Methods

Below is a categorized explanation of important string methods with clear examples.

Case Conversion Methods

capitalize()

Makes only the first character uppercase.

Try it Yourself

lower() and upper()

Convert strings to lowercase or uppercase.

Try it Yourself

swapcase()

Switches uppercase letters to lowercase and vice versa.

Try it Yourself

title()

Capitalizes the first letter of each word.

Try it Yourself

Searching & Counting Methods

count()

Counts how many times a substring appears.

Try it Yourself

find() and rfind()

Find the position of a substring.

Try it Yourself

index()

Similar to find() but throws an error if not found.

Try it Yourself

Trimming & Alignment Methods

strip(), lstrip(), rstrip()

Remove unwanted spaces from the beginning or end.

Try it Yourself

center(), ljust(), rjust()

Align text within a given width using a character.

Try it Yourself

Joining & Splitting Methods

split()

Split text into a list based on a delimiter.

Try it Yourself

join()

Join elements of a list into a single string.

Try it Yourself

splitlines()

Split text at line breaks.

Try it Yourself

Validation & Checking Methods

isalnum(), isalpha(), isdigit()

Check if the string consists of alphanumeric, alphabetic, or digit characters.

Try it Yourself

islower() and isupper()

Check if all characters are in lowercase or uppercase.

Try it Yourself

startswith() and endswith()

Check how a string begins or ends.

Try it Yourself

Replacement & Translation Methods

replace()

Replace occurrences of a substring with another.

Try it Yourself

partition()

Split into three parts: (before, separator, after).

Try it Yourself

translate() and maketrans()

Replace multiple characters at once using a translation table.

Try it Yourself

Formatting & Padding

format()

Insert values into placeholders within a string.

Try it Yourself

zfill()

Pad a numeric string with zeros on the left until a specific length is reached.

Try it Yourself
Our website uses cookies to enhance your experience. Learn More
Accept !