String Methods
Saravanan
Posted on February 16, 2023
There are three different types of strings in python. They are
- string literals
- raw strings
- formatted strings
String literals - String literals can be defined using single quotes '
, double quotes "
and triple quotes """
. The escape characters are mentioned inside using a backslash \
for example a new line would be \n
.
Raw strings - In raw string a backslash is treated as a literal character. A string is treated as a raw string of its prefixed with an r
in front. Raw strings are used for regular expressions.
Formatted strings - Also called f-strings allows us to insert values of variables into a string. The value of the variable or expression inside curly braces will replace the curly brace section inside the f-string. Placing the semicolon :
after the variable inside the curly brace will help change the format specification.
String concatenation
String concatenation can be done by both the +
operator and the join function and f-string. In terms of performance, the f-string is the fastest one, followed by the join and plus operators the last.
Common String methods
capitalize() - capitalizes the first character of a string and all others in lowercase.
"first Second".capitalize() # returns "First second"
center(width[, fillchar]) - returns a string in the center within a specified width.
"word".center(10, "-") # returns "---word---"
count(sub[, start[, end]]) - returns the number of occurrences of a substring within a string.
"hello".count("l") # returns 2
find(sub[, start[, end]]) - returns the lowest index of the substring within the string.
"hello world".find("o") # returns 4
format(*args, **kwargs) - performs string formatting operation.
"Hi {}, It's {} now.".format("name", 1 + 1) # returns "Hi name, It's 2 now."
format_map(mapping) - formats a string using a dictionary.
"Hi {name}, It's {time} now.".format_map({"name": "Batman", "time": 12}) # returns "Hi Batman, It's 12 now."
index(sub[, start[, end]]) - returns the lowest index of the substring within the string.
"hello".index("o") # returns 4
isalnum() - returns True if all characters in the string are alphanumeric.
"a1".isalnum() # returns True
isalpha() - returns True if all characters in the string are alphabetic.
"hello".isalpha() # returns True
isdecimal() - returns True if all characters in the string are decimal. Decimal characters are those that can be used to form numbers in base 10.
"123".isdecimal() # returns True
isdigit() - returns True if all characters in the string are digits. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits.
"¹²³".isdigit() # returns True
isnumeric() - returns True if all characters in the string are numeric. Numeric characters include digit characters, and all characters that have the Unicode numeric value property.
"ⅠⅩⅤⅬⅭⅮⅯ".isnumeric() # returns True
islower() - returns True if all characters in the string are lowercase.
"hello".islower() # returns True
isupper() - returns True if all characters in the string are uppercase.
"HELLO".isupper() # returns True
join(iterable) - returns a string concatenated with the elements of an iterable.
" ".join(["Hello", "World"]) # returns "Hello World"
lower() - converts the string to lowercase.
"Hello".lower() # returns "hello"
replace(old, new[, count]) - returns a string with all occurrences of the old string replaced with the new string.
"Hi First".replace("First", "New") # returns "Hello New"
split([sep[, maxsplit]]) - returns a list of the words in the string.
"Hello World".split() # returns ["Hello", "World"]
strip() - returns string with leading and trailing whitespace removed.
"hello ".strip() # returns "hello"
upper() - converts the string to uppercase.
"hello".upper() # returns "HELLO"
Posted on February 16, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.