Python String Methods with Examples
capitalize()
Returns a copy of the string with its first character capitalized.
Examples:
"hello world".capitalize() # Output: "Hello world"
casefold()
Returns a casefolded copy of the string. Casefolded strings are suitable for caseless comparisons.
Examples:
"HELLO".casefold() # Output: "hello"
"ß".casefold() # Output: "ss"
center()
Returns a centered string of a specified width.
Examples:
"hello".center(10) # Output: " hello "
"python".center(20, "-") # Output: "-------python-------"
count()
Returns the number of occurrences of a substring in the string.
Examples:
"banana".count("a") # Output: 3
"hello world".count("o", 5, 11) # Output: 1
encode()
Encodes the string using the specified encoding.
Examples:
"hello".encode() # Output: b'hello'
"ñ".encode("utf-8") # Output: b'\xc3\xb1'
endswith()
Returns True if the string ends with the specified suffix.
Examples:
"python".endswith("on") # Output: True
"hello".endswith("he") # Output: False
expandtabs()
Replaces tab characters with spaces, using a specified tab size.
Examples:
"abc".expandtabs(4) # Output: "a b c"
find()
Returns the lowest index of the substring if found; otherwise, -1.
Examples:
"hello".find("e") # Output: 1
"hello".find("x") # Output: -1
format()
Formats the string using placeholders.
Examples:
"My name is {}.".format("Eshan") # Output: "My name is Eshan."
"{0} + {1} = {2}".format(2, 3, 5) # Output: "2 + 3 = 5"
index()
Returns the lowest index of the substring; raises an exception if not found.
Examples:
"hello".index("e") # Output: 1
"hello".index("x") # Raises ValueError
isalnum()
Returns True if all characters in the string are alphanumeric.
Examples:
"abc123".isalnum() # Output: True
"abc 123".isalnum() # Output: False
isalpha()
Returns True if all characters in the string are alphabetic.
Examples:
"hello".isalpha() # Output: True
"hello123".isalpha() # Output: False
isdigit()
Returns True if all characters in the string are digits.
Examples:
"12345".isdigit() # Output: True
"123abc".isdigit() # Output: False
islower()
Returns True if all characters in the string are lowercase.
Examples:
"hello".islower() # Output: True
"Hello".islower() # Output: False
isnumeric()
Returns True if all characters in the string are numeric.
Examples:
"123".isnumeric() # Output: True
"½".isnumeric() # Output: True
"abc".isnumeric() # Output: False
isspace()
Returns True if all characters in the string are whitespace.
Examples:
" ".isspace() # Output: True
"hello".isspace() # Output: False
istitle()
Returns True if the string follows title case.
Examples:
"Hello World".istitle() # Output: True
"hello world".istitle() # Output: False
isupper()
Returns True if all characters in the string are uppercase.
Examples:
"HELLO".isupper() # Output: True
"Hello".isupper() # Output: False
join()
Concatenates elements of an iterable with the string as separator.
Examples:
"-".join(["a", "b", "c"]) # Output: "a-b-c"
"".join(["1", "2", "3"]) # Output: "123"
ljust()
Left-aligns the string using a specified width.
Examples:
"hello".ljust(10) # Output: "hello "
"world".ljust(10, "-") # Output: "world-----"
lower()
Returns a copy of the string in lowercase.
Examples:
"HELLO".lower() # Output: "hello"
lstrip()
Removes leading whitespace from the string.
Examples:
" hello".lstrip() # Output: "hello"
"--world--".lstrip("-") # Output: "world--"
partition()
Splits the string at the first occurrence of the separator.
Examples:
"hello world".partition(" ") # Output: ("hello", " ", "world")
"hello".partition("x") # Output: ("hello", "", "")
replace()
Replaces occurrences of a substring with another substring.
Examples:
"banana".replace("a", "o") # Output: "bonono"
"apple".replace("p", "b", 1) # Output: "abble"
rfind()
Returns the highest index of the substring if found; otherwise, -1.
Examples:
"hello".rfind("l") # Output: 3
"hello".rfind("x") # Output: -1
rindex()
Returns the highest index of the substring; raises an exception if not found.
Examples:
"hello".rindex("l") # Output: 3
"hello".rindex("x") # Raises ValueError
rjust()
Right-aligns the string using a specified width.
Examples:
"hello".rjust(10) # Output: " hello"
"world".rjust(10, "-") # Output: "-----world"
rstrip()
Removes trailing whitespace from the string.
Examples:
"hello ".rstrip() # Output: "hello"
"--world--".rstrip("-") # Output: "--world"
split()
Splits the string at the specified separator.
Examples:
"a,b,c".split(",") # Output: ["a", "b", "c"]
"hello world".split() # Output: ["hello", "world"]
splitlines()
Splits the string at line breaks.
Examples:
"hello\nworld".splitlines() # Output: ["hello", "world"]
startswith()
Returns True if the string starts with the specified prefix.
Examples:
"python".startswith("py") # Output: True
"hello".startswith("he") # Output: True
strip()
Removes leading and trailing whitespace from the string.
Examples:
" hello ".strip() # Output: "hello"
"--world--".strip("-") # Output: "world"
swapcase()
Swaps case for all characters in the string.
Examples:
"Hello".swapcase() # Output: "hELLO"
title()
Converts the string to title case.
Examples:
"hello world".title() # Output: "Hello World"
translate()
Returns a string where specified characters are replaced.
Examples:
"hello".translate({ord("h"): "y", ord("o"): "a"}) # Output: "yella"
upper()
Returns a copy of the string in uppercase.
Examples:
"hello".upper() # Output: "HELLO"
zfill()
Fills the string with zeros to the specified width.
Examples:
"42".zfill(5) # Output: "00042"