Python Ten frequently used String Method with example

Akash Deep
Analytics Vidhya
Published in
6 min readSep 1, 2020

--

Python Strings

In this article we will try to understand the use of strings and all the options that can be used to avoid writing complex code. String is the powerful class in python having lots of method. We will try to understand few of the methods which are useful and also if you want to explore more you can visit the python official documentation

Before going ahead one thing we need to keep in mind, all the string methods will always returns the new value that means it does not manipulate or change the old values. We will look each and every methods one by one.

1. count()

Count() basically returns the number of times any specified values that appears in the given string. For ex : Suppose we have a sentence “The people of India works for the development of the country India”. we want to count the repetation of the word of “India”, we can make use of string count method. There are 3 parameters associated with the count method and these are listed below

Value: This is the word which we have to search in the given string. In our example its “India”. This is the Required parameter in the count method.

Start: This is the starting index from where we want search of our word in a sentence starts. This is the optional parameter in the count method.

End: This is the end index where the search of word ends. Also, this is the optional parameter in the count method.

String count() method

2. center()

This is also one of the most useful method which we can use. Center method aligns the word to the center with the character that we will provide as argument and if we will not provide any argument it will replace with whitespaces. its syntax is str.center(length, fillchar)

String center() function

3) find()

This is the string method that returns the lowest index of the given word into the find method. The syntax for the find() method is str.find(value, start, end). In the below example we can see the lowest index of India has been returned as 14.

String find() function

4) swapcase()

This is also one of the most important string method which is used to convert lowercase to uppercase and vice-versa. The syntax of the string swapcase is str.swapcase(). The example to use the swapcase is below. All the capital case changed to small case and vice-versa

String Swapcase method

5) startswith() and endswith()

startswith(): The return type of the startswith() is boolean. Suppose if we need to check if any sentence starts with some particular word we can use this string method and if the sentence starts with that word it will return true. The syntax of this startswith() method is str.startswith(value,start,end). In the second example we had specified the string index where needs to be checked and i.e 4

startswith() string method

endswith(): The return type of endswith() is also boolean. Suppose if we need to check if any sentence ends with some particular word, we can use this string method and if the sentence ends with that word it will return true. The syntax of this endswith() method is str.endswith(value,start,end). This can be used like this.

endswith() string method

6) split()

Split is the most used method. Remember we had used split while using pre trained word embeddings. Split return list of the words with whitespace as a default separator. The syntax of the split() method is str.split(sep, maxsplit).

sep: The separator which will be used for the splitting of the string. If not specified anything by default it will be white space. This is the optional parameter

maxsplit: It denotes the number of split. By default it will be -1 that means all the words will be splited. If we will specify maxsplit as 2, there will be only 2 splits. Below example explains the maxsplit.

String split method

7) For Capitalize String we have capitalize(), upper(), string.title()

We will try to understand all the 3 methods one by one.

a) capitalize()

This method capitalize only the first character of string. Syntax for this is str.capitalize()

b) upper()

This method capitalize all the characters of the string. Syntax for this is str.upper()

c) title()

This method capitalize all the first letter of the given string. Syntax for this is str.title(). We will look at the 3 methods in below code.

title(), upper(), capitalize() string method

8) ljust() and rjust():

This two methods returns the left and right justified alignment. The syntax for ljust() and rjust() is str.ljust(length,character). We will try to understand the parameters one by one

length: Total length of the string which needs to be returned

character: If we want to fill any character in the returned blank space, we can specify as a character. suppose we had applied ljust as 25 and our string length is 12 and remaining field i need to fill with letter ‘a’ i can specify character as ‘a’. We will see example code for the both ljust() and rjust().

ljust() and rjust() string function

9) strip()

strip() is the most common used string method. It is used to remove the specified character as attribute in the strip method. It also has 2 flavors name as lstrip() and rstrip(). Suppose we need to remove ‘!’ or any character from the string we can make use of this strip() method. we will look lstrip and rstrip one by one. One thing we need to make a note it doesn’t looks the specified character in the middle of the the any 2 words

lstrip(): it removes the character from the left of the string

rstip(): It removes the character from the right of the string

We will look all the flavors one by one using python code

strip() string methods

10) zfill()

This methods add 0 at the starting of the given string. It accepts one argument width. If the width matches the original string it returns the same string back. syntax for zfill is str.zfill(width). We will see the implementation in below code.

zfill() string method

Congratulations, we had learnt the most commonly used string methods in python. these all methods we can use in data preprocessing or we can use in scriptings. I thought of sharing this useful methods because recently i had used these all methods while developing one python script and i am very thankful for these string methods. There are many methods in string to understand all the methods i will suggest to look after the python documentation.

If you have any doubts in understanding of any of these method or any string methods, please comment below i will happy to address. In the next article i will be explain some other utilities of python which will be very useful in day to day works. If you are interested in deep learning i suggest you to check some articles written by me. I had explained the concepts step by step there. I will attach the article below.

Stay Tuned

--

--