Strip or Trim a String
To strip or trim any white space character(s) present at the start or end of a given string, use the method strip() on the string.
In this tutorial, we will learn the syntax of strip() function and how to use this function to strip specific characters from the ends of the given string.
Syntax
The syntax of strip() function is
string.strip(characters)
where specified characters
has to be trimmed off from leading and trailing edges of the string
.
Parameter characters
is optional. If nothing is passed as argument to the strip() method, the set of white space characters is considered as characters
.
Note: strip() function does not affect any white space between the non-white space characters. We will explain this in the following examples.
Examples
Strip or Trim White Spaces at Start and End
In the following basic example, we demonstrate the usage of strip() method. We take a string that has some single white spaces at the start and end of the string.
Example.py
#the string str = ' Hello TutorialKart ' #strip any white characters strippedStr = str.strip() print("Original String\n---------------") print(str) print("\nStripped String\n---------------") print(strippedStr)Try Online
We have two white spaces at the start of the string and three white spaces at the end. When we apply strip() function on this string, these spaces are removed. The white space between Hello
and TutorialKart
is untouched by the strip() method.
Output
Original String --------------- Hello TutorialKart Stripped String --------------- Hello TutorialKart
Strip Specified Character(s)
In this example, we will take a string and strip any character(s) that are present in the characters
argument passed to strip().
Example.py
#the string str = '---Hello TutorialKart@-@' characters = '-@' #strip the string strippedStr = str.strip(characters) print("Original String\n---------------") print(str) print("\nStripped String\n---------------") print(strippedStr)Try Online
The characters -
and @
and any if provided will be removed from beginning and end of this string .
Output
Original String --------------- ---Hello TutorialKart@-@ Stripped String --------------- Hello TutorialKart
Strip or Trim Tabs and Newlines at Start and End
In this example, we will take a string that has newline and tab space characters at the start of the string.
Example.py
#the string str = ' \n\tHello TutorialKart ' #strip any white space characters strippedStr = str.strip() print("Original String\n---------------") print(str) print("\nStripped String\n---------------") print(strippedStr)Try Online
The newline and tab space characters are stripped off.
Output
Original String --------------- Hello TutorialKart Stripped String --------------- Hello TutorialKart
Strip White Space Character(s)
In this example, we will take a string that has newline and tab space characters at the start and in between non-white space characters of the string.
Example.py
#the string str = ' \n\tHello\n\tTutorialKart ' #strip any white space characters strippedStr = str.strip() print("Original String\n---------------") print(str) print("\nStripped String\n---------------") print(strippedStr)Try Online
The newline and tab space characters present only at the start (and end) of the string are stripped off.
Output
Original String --------------- Hello TutorialKart Stripped String --------------- Hello TutorialKart
Conclusion
In this Python Tutorial, we learned to strip or trim any white space characters that are present at start or end of the string using strip() method.