Built-in String Functions in Python

Built-in String Functions in Python

2 min read

Python treats strings as an object. It offers various methods that can be used to get our work done in no(or less) time.
There are several built-in functions available that allows us to modify and (or) manipulate data easily and efficiently.

Python offers a function dir which lists all the available methods in the particular object.

# This lists all methods available in class 'str'
>>>dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

As there are many methods available we will discuss only a few important.

Some Important String Methods

1. len()

This method returns the length of the String.
Syntax:-
len(string)

2.capitalize()

This function creates the exact copy of string with the first letter in upper case.
Syntax:-
string.capitalize()

3.replace()

This function replaces all the occurrences of the old string with the new string. It takes two parameters.
Syntax:-
string.replace(old_string,new_string)

4.split()

This method breaks up a string by a specific separator and returns a list of sub-strings. It takes two parameters.
Syntax:-
string.split(separator,maxsplit)
Here both the parameters are optional.
Separator is used to give the sub-string which is to be used as a delimiter. If the separator is not specified, any white space, newline etc. are treated as a separator.
Maxsplit defines the maximum no. of splits. By default, maxsplit is -1(which means no limit.)

>>>word="Hello!"
>>>print(len(word))
6
>>>string='This is Ram\'s book.'# Here \' is treated as a single character.
19
>>>s1="python"
>>>print(s1.capitalize())
Python
>>>print(string.replace(' is ',' was '))
#Here note that extra space is used so that the function don't change 'This' in 'Thwas'
This was Ram's book.
>>>item='Tea:Coffee:Oil:Shampoo'
>>>print(item.split(':',2))
['Tea','Coffee','Oil:Shampoo']
>>>print(item.split(sep=':',1))
['Tea','Coffee:Oil:Shampoo']
Important
  • String methods do not modify the original string. Rather they make a fresh copy of it.
  • We can use sep=”separator” in split function to define the separator when the maxsplit need not be defined.
Tags:
Choose your Reaction!
Leave a Comment