Python Tutorial – String Manipulation

Python Tutorial on String Manipulation - Lower, Upper, Slice operations, Extract by Index, Split, Strip, Join, Title, Capitalize, Count

In this Python Tutorial, we are going to learn most used string manipulation techniques.

Python Tutorial - String Functions

Lower Case:

name = "Rohan"
print(name.lower())
rohan

Upper Case:

name = "Rohan"
print(name.upper())
ROHAN

Check whether a string is lower or not:

c = "TEST"
c.islower()

False

Check whether a string is upper or not:

c = "TEST"
c.isupper()
True

Replace:

name = "Puff is a magic dragon."
print(name.replace("dragon","dog"))
Puff is a magic dog.

Extract by Index:

As a String is an array of characters (i.e. a collection of characters), each character is assigned with an index.

String index will start from 0. So, for example, the charater in 4th place will have an index of 3.

The index can be used to retrieve a particular charater.

# To find the index of a string use index() function
story = "the Puff the Dragon story is good. but It has a sad ending."
r1.index("Puff")
4
name = "Rohan"
print(name[3])
a

Slice Operation:

Slicing is a Technique to extract a part of the sequence of elements.

Syntax for Slice operation:

string_name[start:stop:step]

Start-> From where the extract starts

Stop -> where the extract stops

Start index will be included, Stop index will be excluded

Default start is 0, if not provided it will be 0

Default stop is end of list

This slicing operation can be used to get a substring from a String.

name = "This is an example for Python String Slicing."
print(name[3:])
s is an example for Python String Slicing.
numbers = "1,2,3,4,5,6,7,8,9"
print(numbers[0::2])
123456789

Split:

  • Split() function takes a character or string as input and use that string as a separator to split a string into multiple parts.
  • The input can be a single character or a string.
  • If no input is given, then the default input parameter is space.
  • The multiple parts will be returned as a list of strings.
  • So this is very useful to iterator over the string based on a custom split.
story = "Puff the magic dragon lived by the sea."
print(story.split())
['Puff', 'the', 'magic', 'dragon', 'lived', 'by', 'the', 'sea.']
story = "Puff the magic dragon lived by the sea."
print(story.split(" "))
['Puff', 'the', 'magic', 'dragon', 'lived', 'by', 'the', 'sea.']
story = "Puff the magic dragon lived by the sea."
words = story.split(" ")
print(type(words))
<class 'list'>
story = "Puff the magic dragon lived by the sea. And frolicked in the autumn mist in a land called Honah Lee. A dragon lives forever, but not so little boys. Painted wings and giant's rings make way for other toys."
print(story.split("."))
['Puff the magic dragon lived by the sea', ' And frolicked in the autumn mist in a land called Honah Lee', ' A dragon lives forever, but not so little boys', " Painted wings and giant's rings make way for other toys", '']

“in” condition:

when we need to find the existence of a word/character in a string, we can use a in “condition”.

story = "Puff the magic dragon lived by the sea."
print("dragon" in story)
print("magic dragon" in story)
print("Dragon" in story)
True
True
False

Strip

Takes in a string/character as a parameter and remove it from a string either in front or back.

If not parameters are specified, then the default value is empty space. It removes leading and trailing spaces.

The variations of this function is lstrip and rstrip.

lstrip removes the characters in front/left area.

rstrip removes the characters in back/right area.

Disadvantage of Strip:

It does not remove string/character/spaces in between the string. It removes only from leading and trailing areas.

r1 = "                Puff the magic dragon lived by the sea. And frolicked in the autumn mist in a land called Honah Lee."
r2 = "Puff the magic dragon lived by the sea. And frolicked in the autumn mist in a land called Honah Lee.             "
r3 = "            Puff the magic dragon lived by the sea.       And frolicked in the autumn mist in a land called Honah Lee.               "
print(r1.lstrip())
print(r2.rstrip())
print(r3.strip())
Puff the magic dragon lived by the sea. And frolicked in the autumn mist in a land called Honah Lee.
Puff the magic dragon lived by the sea. And frolicked in the autumn mist in a land called Honah Lee.
Puff the magic dragon lived by the sea.       And frolicked in the autumn mist in a land called Honah Lee.
r3 = "t*******************The HeadPhones are working fine. But I do not like the built."
print(r3.strip('**'))
r3 = "********************The HeadPhones are working fine. But I do not like the built."
print(r3.strip('*'))
t*******************The HeadPhones are working fine. But I do not like the built.
The HeadPhones are working fine. But I do not like the built.
The HeadPhones are working fine. But I do not like the built.

Join and Split

We have seen that the Split function breaks the string.

To join the list of strings again, we can use join function.

The join function also allows us to join using a custom separator.

If no separator is provided, then the join will be continuous without any separator.

This combination overcomes the disadvantage of strip function.

Using this way, we can remove the spaces/characters in-between.

r1 = "            The Puff the Dragon story is good.         But It has a sad ending.               "
" ".join(r1.split())
'The Puff the Dragon story is good. But It has a sad ending.'
list1 = r1.split()
" ".join(list1)
'The HeadPhones are working fine. But I do not like the built.'
"$$".join(list1)
'The$$HeadPhones$$are$$working$$fine.$$But$$I$$do$$not$$like$$the$$built.'
"".join(list1)
'TheHeadPhonesareworkingfine.ButIdonotlikethebuilt.'

Count

Count function returns the number of existence of the string in the string.

r1 = "The Puff the Dragon story is good. But It has a sad ending."
r1.count("a")
4
r1 = "The Puff the Dragon story is good.but It has a sad ending."
r1.title()
'The Puff The Dragon Story Is Good.But It Has A Sad Ending.'

Title

Converts the first character of each word in the given string to upper case.

r1.title()
'The Puff The Dragon Story Is Good. But It Has A Sad Ending.'

Capitalize

Converts the first character of the entire string to capital. It will not change the first character of other words or sentences (comes after a full stop.)

r1 = "The Puff the Dragon story is good. but It has a sad ending."
r1.capitalize()
'The puff the dragon story is good. but it has a sad ending.'

There are a few more string manipulation functions available in Python. You can find everything in the documentation https://docs.python.org/3/library/stdtypes.html#string-methods.

Explore the functions and try it!

Asha Ponraj
Asha Ponraj

Data science and Machine Learning enthusiast | Software Developer | Blog Writter

Articles: 86

Leave a Reply

Your email address will not be published. Required fields are marked *