Python Built-in String Functions
Function Description Syntax Example
cmp() Compares two strings (removed in Python 3)
cmp(str1, str2) cmp('apple', 'banana') -> -1
capitalize() Converts first character to uppercase str.capitalize() 'hello'.capitalize() -> 'Hello'
center() Centers string with padding str.center(width, fillchar) 'hi'.center(10, '*') -> '****hi****'
count() Counts occurrences of a substring str.count(sub) 'banana'.count('a') -> 3
encode() Encodes string into bytes str.encode(encoding) 'hello'.encode('utf-8')
endswith() Checks if string ends with suffix str.endswith(suffix) 'hello'.endswith('lo') -> True
expandtabs() Replaces tabs with spaces str.expandtabs(tabsize) 'hello\tworld'.expandtabs(4)
find() Finds substring index (-1 if not found) str.find(sub) 'hello'.find('e') -> 1
index() Finds substring index (raises error if not found)
str.index(sub) 'hello'.index('e') -> 1
isdecimal() Checks if all chars are decimal str.isdecimal() '123'.isdecimal() -> True
isdigit() Checks if all chars are digits str.isdigit() '123'.isdigit() -> True
isnumeric() Checks if all chars are numeric str.isnumeric() '1/3'.isnumeric() -> True
islower() Checks if all chars are lowercase str.islower() 'hello'.islower() -> True
isupper() Checks if all chars are uppercase str.isupper() 'HELLO'.isupper() -> True
isspace() Checks if all chars are whitespace str.isspace() ' '.isspace() -> True
istitle() Checks if string is titlecased str.istitle() 'Hello World'.istitle() -> True
join() Joins elements of an iterable str.join(iterable) ','.join(['a', 'b']) -> 'a,b'
len() Returns string length len(str) len('hello') -> 5
ljust() Left-aligns string with padding str.ljust(width, fillchar) 'hi'.ljust(5, '-') -> 'hi---'
lower() Converts string to lowercase str.lower() 'HELLO'.lower() -> 'hello'
lstrip() Removes leading whitespace str.lstrip(chars) ' hello'.lstrip() -> 'hello'
max() Returns max character max(str) max('abc') -> 'c'
min() Returns min character min(str) min('abc') -> 'a'
oct() Converts integer to octal string oct(int) oct(8) -> '0o10'
ord() Returns Unicode code of char ord(char) ord('A') -> 65
rstrip() Removes trailing whitespace str.rstrip(chars) 'hello '.rstrip() -> 'hello'
rfind() Finds last occurrence of substring str.rfind(sub) 'banana'.rfind('a') -> 5
rindex() Finds last occurrence index (error if not found)
str.rindex(sub) 'banana'.rindex('a') -> 5
replace() Replaces substring str.replace(old, new) 'hello'.replace('l', 'x') -> 'hexxo'
split() Splits string into list str.split(sep) 'a,b,c'.split(',') -> ['a', 'b', 'c']
startswith() Checks if string starts with prefix str.startswith(prefix) 'hello'.startswith('he') -> True
strip() Removes leading and trailing whitespace str.strip(chars) ' hello '.strip() -> 'hello'
swapcase() Swaps case of characters str.swapcase() 'Hello'.swapcase() -> 'hELLO'
title() Converts string to title case str.title() 'hello world'.title() -> 'Hello World'
upper() Converts string to uppercase str.upper() 'hello'.upper() -> 'HELLO'