In this Python tutorial, we will learn how to trim or strip specific characters from the ends of the given string using string.strip() function.
Python – 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.
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
1. Trim white spaces around string
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.
Python Program
str = ' Hello TutorialKart '
#strip any white characters
strippedStr = str.strip()
print("Original String\n---------------")
print(str)
print("\nStripped String\n---------------")
print(strippedStr)
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
2. Strip specified character(s) from string ends
In this example, we will take a string and strip any character(s) that are present in the characters
argument passed to strip().
Python Program
str = '---Hello TutorialKart@-@'
characters = '-@'
#strip the string
strippedStr = str.strip(characters)
print("Original String\n---------------")
print(str)
print("\nStripped String\n---------------")
print(strippedStr)
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
3. Trim tabs and newlines from string start and end
In this example, we will take a string that has newline and tab space characters at the start of the string.
Python Program
str = ' \n\tHello TutorialKart '
#strip any white space characters
strippedStr = str.strip()
print("Original String\n---------------")
print(str)
print("\nStripped String\n---------------")
print(strippedStr)
The newline and tab space characters are stripped off.
Output
Original String
---------------
Hello TutorialKart
Stripped String
---------------
Hello TutorialKart
4. Strip all white space character(s) from edges of string
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.
Python Program
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)
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.