Blog

Python String Functions

  • (4.0)
  • | 1016 Ratings |
  • Last Updated September 20, 2017

Python String is one of the easiest programming languages to either newbies or seasoned professionals alike. If you are completely new to programming language, you should be able to pick up the concepts at a quicker pace and if you already have some coding knowledge (whatever might be the programming language that you have already learnt), learning Python is just a piece of cake.

If you’re here then we assume that you are very much interested in getting your hands dirty with the Python programming skills, so why delay? Let us deep dive into the basics of the programming language. Considering that you have enough knowledge on the programming language itself and a little coding skills, let’s check all the possible string functions made available for the Python developers.

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

Python String

Strings are the most fascinating subject taken any programming language and Java, in specific as I come from that background. But let me tell you that Python has equal number of functions to manipulate Strings and to make life of Python developers easy. Now with no more further delay, we will try to check the most commonly used String functions in Python programming language.

Following is the list of all built-in functions provided by Python programming language, and out of these we shall cherry pick the ones that are most commonly used in day to day works for String manipulation.

Frequently Asked Python Interview Questions & Answers

Python Programming language

 Function Syntax  Function Description / Explanation
 capitalize()  This function will capitalize the very first letter of the string on which this is invoked
 center(width, fill)  This function will pad the given string with enough number of spaces so that the provided string can be centered (to the number of columns, as provided in the parameter – width).
 count(string,beg=0,end=len(string))  This function counts the number of occurrences of the string string or in a substring, if the beginning index beg and ending index end are given as input parameters to it.
 decode(encoding='UTF-8',errors='strict')  This function decodes the string on which it is invoked using the encoding specified in the input parameter. Encoding is defaulted to the default string encoding, if not specified.
 encode(encoding='UTF-8',errors='strict')  This function returns the encoded string based on the encoding parameter provided as input. Errors out “ValueError” unless errors is given with “ignore” or “replace” options.
 endswith(suffix, beg=0, end=len(string))  This function is used to determine if a string, or a substring of the string provided as an input parameter (if both beginning and ending indices are provided as input), ends with the suffix provided as input as well. Returns TRUE if so or else returns FALSE.
 expandtabs(tabsize=8)  This function expands the tabs in strings to multiple spaces, a default of 8 spaces is used if there is no value specified for tabsize.
 find(str, beg=0 end=len(string))  This function determines whether string str occurs in the string string or in substring of string. If found, then it will return the index of the string str, or else it returns -1.
 index(str, beg=0, end=len(string))  This function is similar as find(), but raises an exception if the string str is not found.
 isalnum()  This function returns TRUE if it at least has one character and if all the characters are alphanumeric, provides FALSE otherwise.
 isalpha()  This function returns TRUE if the string has at least 1 character and if it is alphanumeric, otherwise it sends FALSE.
 isdigit()  This function returns TRUE if the provided string contains only digits and returns FALSE otherwise.
 islower()  This function returns TRUE if the string has at least 1 cased character and if all the characters are in lowercase. It will return FALSE otherwise.
 isnumeric()  Returns true if a unicode string contains only numeric characters and false otherwise.
 isspace()  This function returns TRUE if the string provided has only space characters and returns FALSE if it is not the case.
 istitle()  This function returns TRUE if every letter of the string is properly cased, or else FALSE is returned by it.
 isupper()  This function returns TRUE if the string has at least 1 cased character and if all the characters are in uppercase. It will return FALSE otherwise.
 join(seq)  This function merges the string seq into the provided string, with the separator strings.
 len(string)  This function provides the length of the string in Python
 ljust(width[, fillchar])  This function returns a space padded string with the original string left aligned to a total number of columns as specified in the width parameter.
 lower()   This function converts all the upper cased letters to lower case letters of the string
 lstrip()  This function strips off all the preceding spaces and provides the string.
 maketrans()  This function returns a translation table that would be used in the translate function.
 max(str)  This function returns the maximum alphabetical character from the string str specified in the input parameter.
 min(str)  This function returns the minimum alphabetical character from the string str specified in the input parameter.
 replace(old, new [, max])  This function replaces all the occurrences of the string old with the string new or at most max occurrences if max is provided as input parameter to the function specified.
 rfind(str, beg=0,end=len(string))  This function is similar to find(), but searches backwards on the string specified
 rindex( str, beg=0, end=len(string))  This function is similar to index(), but searches backwards on the string specified
 rjust(width,[, fillchar])  This function returns a padded string with spaces, where the original string is right aligned by the number of columns width, as specified in the function’s argument list.
 rstrip()  This function strips all the trailing spaces on the string specified
 split(str="", num=string.count(str))  This function splits the provided string according to the delimiter specified str, returns the list of all substrings. This splits into utmost num substrings, if num is provided in the arguments list.
 splitlines( num=string.count('n'))  This function splits the provided string at all or at num NEWLINEs and then returns the list of each line with NEWLINEs removed.
 startswith(str, beg=0,end=len(string))  This function determines whether the string or a substring of it (provided the beginning and the ending indices of the string), starts with the string str and returns TRUE if it identifies or else returns FALSE.
 strip([chars])  This function is equivalent to performing both the lstrip() and rstrip() functions on the string provided.
 swapcase()  This function inverts the case of the string on which this method is invoked
 title()  This function returns the title case version of the string, which is the camel case – every first letter of the word is capitalized and the rest are left in the lower case.
 translate(table, deletechars="")  This function translates the string according to the translation table and removing those in characters mentioned in the input parameter.
 upper()  This function converts all the lower cased characters in the string to uppercase
 zfill (width)  This function returns the original string padded with zeros to a total of characters mentioned in the width input parameter. This is specifically intended for numbers.
 isdecimal()  This function does a check and returns TRUE only if a Unicode string contains just decimal characters only and nothing else, otherwise returns FALSE.

 

This article has introduced all the in-built String functions that is provided by Python in specific.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

 


Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Free Demo Popup -->