This section provides you with useful Python string methods that help you manipulate strings more effectively.
Concatenating & Splitting strings
These methods allow you to concatenate strings into one string and split a string into substrings effectively.
Method | Description |
---|---|
join() | Return a string that is the concatenation of strings in an iterable. |
concat | Return a string that is the result of a concatenation of two or more strings. |
split() | Return a list of substring split from the string by a delimiter. |
Finding substrings
These methods allow you to locate a substring in a string and return the index of the substring.
Method | Description |
---|---|
index() | Return the index of a substring in a string or raise a ValueError if the substring not found. |
find() | Return the index of the first occurrence of a substring in a string or -1 if the substring not found. |
startswith() | Return True if a string starts with another string. |
endswith() | Return True if a string ends with another string. |
Dealing with string cases
These methods help you deal with string cases effectively including lowercase, uppercase, titlecase, and casefold.
Method | Description |
---|---|
lower() | Return a copy of a string in lowercase |
upper() | Return a copy of a string in uppercase. |
title() | Return a copy of a string in the titlecase. |
capitalize() | Return a string with the first letter converted to uppercase and other letters converted to lowercase. |
islower() | Return True if all cased characters in the string are lowercase and there’s at least one cased character. |
istitle() | Return True if the string follows the title case rules. |
isupper() | Return True if all the cased characters in the string are uppercase and there’s at least one cased character. |
casefold() | Return a casefolded copy of the string. |
swapcase() | Return a copy of the string with all lowercase characters converted to uppercase and vice versa |
Removing unwanted characters
These methods allow you to remove leading and/or trailing unwanted characters from a string and return a new string.
Method | Description |
---|---|
lstrip() | Return a copy of a string with leading characters removed. |
rstrip() | Return a copy of a string with trailing characters removed. |
strip() | Return a copy of a string with leading and trailing characters removed. |
Replacing substrings
Method | Description |
---|---|
replace() | Return a copy of a string with some or all occurrences of a substring replaced with a new one. |
Checking characters in Strings
Method | Description |
---|---|
isdigit() | Return True if all characters in a string are digits. |
isdecimal() | Return True if all characters in a string are decimal characters. |
isnumeric() | Return True if all characters in a string are numeric characters. |
isalpha() | Return True if all characters in a string are alphabetic characters. |
isalnum() | Return True if all characters in a string are alphanumeric characters. |
Did you find this tutorial helpful ?