July 13th, 2019

ListenData: String Functions in Python with Examples

Python, by admin.
This tutorial outlines various string (character) functions used in Python. To manipulate strings and character values, python has several in-built functions. It means you don’t need to import or have dependency on any external package to deal with string data type in Python. It’s one of the advantage of using Python over other data science tools. Dealing with string values is very common in real-world. Suppose you have customers’ full name and you were asked by your manager to extract first and last name of customer. Or you want to fetch information of all the products that have code starting with ‘QT’.

Table of Contents

Python String Functions

List of frequently used string functions

The table below shows many common string functions along with description and its equivalent function in MS Excel. We all use MS Excel in our workplace and familiar with the functions used in MS Excel. The comparison of string functions in MS EXCEL and Python would help you to learn the functions quickly and mug-up before interview.

Function Description MS EXCEL FUNCTION
mystring[:N] Extract N number of characters from start of string. LEFT( )
mystring[-N:] Extract N number of characters from end of string RIGHT( )
mystring[X:Y] Extract characters from middle of string, starting from X position and ends with Y MID( )
str.split(sep=’ ‘) Split Strings
str.replace(old_substring, new_substring) Replace a part of text with different sub-string REPLACE( )
str.lower() Convert characters to lowercase LOWER( )
str.upper() Convert characters to uppercase UPPER( )
str.contains(‘pattern’, case=False) Check if pattern matches  (Pandas Function) SQL LIKE Operator
str.extract(regular_expression) Return matched values (Pandas Function)
str.count(‘sub_string’) Count occurence of pattern in string
str.find( ) Return position of sub-string or pattern FIND( )
str.isalnum() Check whether string consists of only alphanumeric characters
str.islower() Check whether characters are all lower case
str.isupper() Check whether characters are all upper case
str.isnumeric() Check whether string consists of only numeric characters
str.isspace() Check whether string consists of only whitespace characters
len( ) Calculate length of string LEN( )
cat( ) Concatenate Strings (Pandas Function) CONCATENATE( )
separator.join(str) Concatenate Strings CONCATENATE( )


About Author:

Deepanshu founded ListenData with a simple objective – Make analytics easy to understand and follow. He has over 8 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains.

Let’s Get Connected: LinkedIn

Back Top

Comments are closed.