DEV Community

Saravanan
Saravanan

Posted on

String Methods

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"
Enter fullscreen mode Exit fullscreen mode

center(width[, fillchar]) - returns a string in the center within a specified width.

"word".center(10, "-") # returns "---word---"
Enter fullscreen mode Exit fullscreen mode

count(sub[, start[, end]]) - returns the number of occurrences of a substring within a string.

"hello".count("l") # returns 2
Enter fullscreen mode Exit fullscreen mode

find(sub[, start[, end]]) - returns the lowest index of the substring within the string.

"hello world".find("o") # returns 4
Enter fullscreen mode Exit fullscreen mode

format(*args, **kwargs) - performs string formatting operation.

"Hi {}, It's {} now.".format("name", 1 + 1) # returns "Hi name, It's 2 now."
Enter fullscreen mode Exit fullscreen mode

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."
Enter fullscreen mode Exit fullscreen mode

index(sub[, start[, end]]) - returns the lowest index of the substring within the string.

"hello".index("o") # returns 4
Enter fullscreen mode Exit fullscreen mode

isalnum() - returns True if all characters in the string are alphanumeric.

"a1".isalnum() # returns True
Enter fullscreen mode Exit fullscreen mode

isalpha() - returns True if all characters in the string are alphabetic.

"hello".isalpha() # returns True
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

islower() - returns True if all characters in the string are lowercase.

"hello".islower() # returns True
Enter fullscreen mode Exit fullscreen mode

isupper() - returns True if all characters in the string are uppercase.

"HELLO".isupper() # returns True
Enter fullscreen mode Exit fullscreen mode

join(iterable) - returns a string concatenated with the elements of an iterable.

" ".join(["Hello", "World"]) # returns "Hello World"
Enter fullscreen mode Exit fullscreen mode

lower() - converts the string to lowercase.

"Hello".lower() # returns "hello"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

split([sep[, maxsplit]]) - returns a list of the words in the string.

"Hello World".split() # returns ["Hello", "World"]
Enter fullscreen mode Exit fullscreen mode

strip() - returns string with leading and trailing whitespace removed.

"hello  ".strip() # returns "hello"
Enter fullscreen mode Exit fullscreen mode

upper() - converts the string to uppercase.

"hello".upper() # returns "HELLO"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)