Strings Methods in Python

String class i.e str provides many useful methods to manipulate the string. Specifically, we will discuss methods which do the following.

  1. Search for a substring inside a string.
  2. Test strings.
  3. Format strings.
  4. Convert strings.

Recall from the earlier chapter that methods are functions which belongs to an object. However, unlike a function, a method is always called on an object using the following notation.

Okay, Let’s get started.

Testing Strings

The following methods of the str class tests various types of characters inside the string.

Method Description
str.isalnum() returns True if all the characters in the string is alphanumeric (a string which contains either number or alphabets or both). Otherwise False.
str.isalpha() returns True if all the characters in the string are alphabets. Otherwise False.
str.isdigit() returns True if all the characters in the string are digits. Otherwise False.
str.islower() returns True if all the characters in the string are in lowercase. Otherwise False.
str.isupper() returns True if all the characters in the string are in uppercase. Otherwise False.
str.isspace() returns True if all the characters in the string are whitespace characters. Otherwise False.

Here are some examples:

isalnum() method

isalpha() method

isdigit() method

islower() and isupper() method

isspace() method

Searching and Replacing Strings

The str class has following methods which allow you to search for a substring inside a string.

Method Description
endswith(sub) Returns True if a string ends with substring sub. Otherwise False.
startswith(sub) Returns True if string starts with substring sub. Otherwise False.
find(sub) Returns the lowest index of the string where substring sub is found. If substring sub is not found -1 is returned.
rfind(sub) Returns the highest index of the string where substring sub is found. If substring sub is not found -1 is returned.
count(sub) It returns the number of occurrences of substring sub found in the string. If no occurrences found 0 is returned.
replace(old, new) It returns a new string after replacing old substring with new. Notice that it does not change the object on which it is called.

Some examples:

Converting Strings

The following methods are commonly used to return a modified version of the string.

Method Description
lower() Returns a new copy of the string after converting all of it's characters to lowercase.
upper() Returns a new copy of the string after converting all of it's characters to uppercase.
capitalize() Returns a new copy of the string after capitalizing only the first letter in the string.
title() Returns a new copy of the string after capitalizing the first letter in each word.
swapcase() Returns a new copy after converting lowercase letters to uppercase and vice-versa.
strip() Returns a new copy of the string after removing all the leading and trailing whitespace characters.
strip(chars) Returns a new copy of the string after removing chars from the beginning and end of the string.

Always remember that these methods return a new string object and do not modify the original string object in any way.

Here are some examples:

lower() method

upper() method

capitalize() and title() method

swapcase() method

strip() method

Formatting Methods

The following table list some formatting methods of the str class.

Method Description
center(width) Returns a new copy of the string after centering it in a field of length width.
ljust(width) Returns a new copy of the string justified to left in the field of length width.
rjust(width) Returns a new copy of the string justified to right in the field of length width.

center() method

ljust() method

rjust() method

Leave a Comment

%d bloggers like this: