0% found this document useful (0 votes)
6 views2 pages

Python String Functions CheatSheet

The document lists the most commonly used string functions in Python, categorized into various sections such as String Case Functions, Search & Check, Check Types, Modify or Clean, Split & Join, and Formatting. Each function is described with its purpose, such as converting case, searching substrings, checking character types, modifying strings, splitting and joining, and formatting. This serves as a quick reference guide for Python string manipulation.

Uploaded by

arunimajoshi3110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python String Functions CheatSheet

The document lists the most commonly used string functions in Python, categorized into various sections such as String Case Functions, Search & Check, Check Types, Modify or Clean, Split & Join, and Formatting. Each function is described with its purpose, such as converting case, searching substrings, checking character types, modifying strings, splitting and joining, and formatting. This serves as a quick reference guide for Python string manipulation.

Uploaded by

arunimajoshi3110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Most Used String Functions in Python

String Case Functions

`str.lower()` - Converts all characters to lowercase.

`str.upper()` - Converts all characters to uppercase.

`str.capitalize()` - Capitalizes the first character.

`str.title()` - Capitalizes the first letter of each word.

`str.swapcase()` - Swaps case of all characters.

Search & Check

`str.find(sub)` - Returns lowest index of `sub`, or `-1` if not found.

`str.rfind(sub)` - Like `find()`, but searches from the end.

`str.index(sub)` - Like `find()`, but raises `ValueError` if not found.

`str.startswith(prefix)` - Returns `True` if string starts with `prefix`.

`str.endswith(suffix)` - Returns `True` if string ends with `suffix`.

`str.count(sub)` - Counts how many times `sub` appears.

Check Types

`str.isalpha()` - Checks if all characters are alphabetic.

`str.isdigit()` - Checks if all characters are digits.

`str.isalnum()` - Checks if all characters are alphanumeric.

`str.isspace()` - Checks if all characters are whitespace.

`str.islower()` - Checks if all letters are lowercase.

`str.isupper()` - Checks if all letters are uppercase.

Modify or Clean

`str.strip()` - Removes leading and trailing whitespace.

`str.lstrip()` - Removes leading whitespace.

`str.rstrip()` - Removes trailing whitespace.

`str.replace(old, new)` - Replaces occurrences of `old` with `new`.


Most Used String Functions in Python

Split & Join

`str.split(sep)` - Splits the string into a list by `sep`.

`str.rsplit(sep)` - Splits from the right.

`str.splitlines()` - Splits string at line breaks.

`'sep'.join(list)` - Joins elements of a list into a string with separator `sep`.

Formatting

`str.format()` - Used for string formatting (e.g., `'Hello {}'.format(name)`)

`f'{}'` - f-strings (Python 3.6+), e.g., `f'Hello {name}'`.

You might also like