String methods · Python Reference
Strings are immutable, so every string method returns a new string instead of changing the original. These pages explain the most useful methods for cleaning, searching, splitting and formatting text, each with a runnable example, the exact return value and the errors to avoid.
All 24
Python String split() Method
`str.split(sep=None, maxsplit=-1)` cuts a string into pieces and returns them as a **list of strings**; with no `sep` it splits o…
Python String join() Method`separator.join(iterable)` glues every string in the iterable together into **one string**, placing `separator` between neighbour…
Python String strip() Method`text.strip(chars=None)` returns a **new string** with leading and trailing whitespace removed, or with any characters listed in …
Python String lstrip() Method`text.lstrip(chars=None)` returns a new string with **leading** (left-hand) whitespace, or any characters in `chars`, removed; th…
Python String rstrip() Method`text.rstrip(chars=None)` returns a new string with **trailing** (right-hand) whitespace, or any characters in `chars`, removed; …
Python String replace() Method`text.replace(old, new, count=-1)` returns a **new string** in which every occurrence of `old` is replaced by `new`, or only the …
Python String upper() Method`text.upper()` returns a **new string** with every lowercase letter converted to uppercase; digits, spaces and symbols stay the s…
Python String lower() Method`text.lower()` returns a **new string** with every uppercase letter converted to lowercase; characters without case are unchanged.
Python String title() Method`text.title()` returns a **new string** in which the first letter of every word is uppercase and all other letters are lowercase.
Python String capitalize() Method`text.capitalize()` returns a **new string** with the first character in uppercase and **all remaining characters in lowercase**.
Python String swapcase() Method`text.swapcase()` returns a **new string** in which every uppercase letter becomes lowercase and every lowercase letter becomes u…
Python String find() Method`text.find(sub, start=0, end=len(text))` returns the **lowest index** where `sub` occurs, or **`-1`** if it does not occur; it ne…
Python String index() Method`str.index(sub)` returns the position of the **first** occurrence of `sub` in a string, and raises `ValueError` if `sub` is not f…
Python String count() Method`str.count(sub)` returns how many **non-overlapping** times `sub` appears in a string. It returns `0` when there is no match and …
Python String startswith() Method`str.startswith(prefix)` returns `True` if the string begins with `prefix` and `False` otherwise. Pass a **tuple** of prefixes to…
Python String endswith() Method`str.endswith(suffix)` returns `True` if the string finishes with `suffix` and `False` otherwise. Pass a **tuple** to test severa…
Python String format() Method`str.format()` fills `{}` placeholders in a string with the values you pass in, for example `"Hi {}".format("Asha")` gives `'Hi A…
Python String isdigit() Method`str.isdigit()` returns `True` if the string is **not empty** and every character is a digit. `"123"` passes; `"-5"`, `"3.14"` an…
Python String isalpha() Method`str.isalpha()` returns `True` if the string is **not empty** and every character is a letter. Spaces, digits, hyphens and punctu…
Python String isalnum() Method`str.isalnum()` returns `True` if the string is **not empty** and every character is a letter or a digit. Spaces, underscores and…
Python String center() Method`str.center(width, fillchar)` returns a new string of length `width` with the original text in the middle, padded on both sides w…
Python String zfill() Method`str.zfill(width)` pads a string on the left with zeros until it is `width` characters long, for example `"42".zfill(5)` gives `'…
Python String splitlines() Method`str.splitlines()` splits a string at line boundaries and returns a list of the lines, without the line-break characters unless y…
Python String partition() Method`str.partition(sep)` splits a string at the **first** occurrence of `sep` and always returns a tuple of three parts: the text bef…
Explore more
Python ProgramsPython ComparisonsPython How-To GuidesPython Cheat SheetsPython glossaryPython tutorial