20 Python String methods to know with example

Hello friends! Today we’ll be learning few Python string methods along with examples.

capitalize(): Converts the first character to uppercase and the rest to lowercase.

string = "hello world"
print(string.capitalize()) # Output: Hello world

casefold(): Converts string into lower case.

string = "HELLO World"
print(string.casefold()) # Output: hello world

count(substring[, start[, end]]): Returns the number of occurrences of a substring in the string.

string = "hello world"
print(string.count('l')) # Output: 3

endswith(suffix[, start[, end]]): Checks if a string ends with a specified suffix.

string = "hello world"
print(string.endswith('world')) # Output: True

find(sub[, start[, end]]): Returns the lowest index where a substring is found.

string = "hello world"
print(string.find('world')) # Output: 6

index(sub[, start[, end]]): Similar to find(), but raises an exception if the substring is not found.

string = "hello world"
print(string.index('world')) # Output: 6

isalnum(): Returns True if all characters in the string are alphanumeric.

string = "hello123"
print(string.isalnum()) # Output: True

isalpha(): Returns True if all characters in the string are alphabetic.

string = "hello"
print(string.isalpha()) # Output: True

isdigit(): Returns True if all characters in the string are digits.

string = "123"
print(string.isdigit()) # Output: True

islower(): Returns True if all characters in the string are lowercase.

string = "hello"
print(string.islower()) # Output: True

isspace(): Returns True if all characters in the string are whitespaces.

string = " "
print(string.isspace()) # Output: True

isupper(): Returns True if all characters in the string are uppercase.

string = "HELLO"
print(string.isupper()) # Output: True

join(iterable): Joins elements of an iterable (such as a list) with the string as a separator.

my_list = ['Hello', 'world']
print(' '.join(my_list)) # Output: Hello world

lower(): Converts all characters in the string to lowercase.

string = "HELLO"
print(string.lower()) # Output: hello

upper(): Converts all characters in the string to uppercase.

string = "hello"
print(string.upper()) # Output: HELLO

replace(old, new[, count]): Replaces occurrences of a substring with another substring.

string = "hello world"
print(string.replace('world', 'universe')) # Output: hello universe

split(sep=None, maxsplit=-1): Splits the string at specified separator and returns a list.

string = "hello world"
print(string.split()) # Output: ['hello', 'world']

strip([chars]): Removes leading and trailing characters (default is whitespace).

string = " hello world "
print(string.strip()) # Output: hello world

startswith(prefix[, start[, end]]): Checks if the string starts with a specified prefix.

string = "hello world"
print(string.startswith('hello')) # Output: True

title(): Converts the first character of each word to uppercase.

string = "hello world"
print(string.title()) # Output: Hello World

These methods should cover a wide range of string manipulation needs in Python.

Keep visiting Analytics Tuts for more tutorials.

Thanks for reading! Comment your suggestions and queries.


Leave a Reply

Your email address will not be published. Required fields are marked *