strip function in Python
In this post, you will learn about the strip() function of Python with examples.
The Python strip() function returns a copy of the string with the leading and trailing (spaces, tabs, or newlines) characters removed.
Syntax of strip() function
str.strip([chars])
The chars argument is a string specifying the set of characters to be removed. If the chars argument is not provided, it defaults to removing white space. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped.
Example of Python strip() function
These are the some example of Python strip() function.
txt1 = "Welcome, John "
txt2 = " How are you? "
txt3 = " What's going on?"
# Leading and trailing whitespaces are removed
print(txt1.strip())
print(txt2.strip())
print(txt3.strip())
Output of the above code -
Welcome, John
How are you?
What's going on?
In the above code, we have taken three string variables and called the strip() function over them. It removed all the leading and trailing whitespaces from all the strings.
Example of strip() with chars argument
When the character of the string in the left or right mismatches with all the characters in the chars argument, it quits eliminating the main characters, otherwise it eliminates them from the specified string. These are the examples-
str = ' etp eetpp tut eeppt '
# Removing leading and trailing whitespaces
print(str.strip())
# All e,t,p characters with whitespaces of string are removed
print(str.strip(' etp'))
# Argument characters are mismatched
# No characters are removed.
print(str.strip('xyz'))
# Removing specified chars
str1 = 'weather is pleasant'
print(str1.strip('we'))
print(str1.strip(' ant'))
Output of the above code -
etp eetpp tut eeppt
u
etp eetpp tut eeppt
ather is pleasant
weather is pleas
In the above code, we first took a string variable and computed the string = ' etp eetpp tut eeppt '. In the first print statement, it only removed the white spaces. In the second print statement, we passed strip over the str and set the argument as ' etp', the Python interpreter traced white spaces and all the e, t, p characters present in the string.
In the third print statement, we passed strip over the str and set the argument as 'xyz'. The Python interpreter searched the characters x, y, z for tracing, if it was not found, it returned the whole string.
After that, we have specified one more string variable, i.e., str1 = 'weather is pleasant'. In the print statement, we have passed the strip() method and passed the argument 'we'. It traced the 'we' from the string and printed the remaining one. Similarly, in the next print statement, it traced the 'ant' and printed the remaining one.
Related Articles
Convert string to list Python
Python remove punctuation from string
Python split string by comma
Count vowels in a string Python
Count consonants in a string Python
Greatest common divisor Python recursive
Python String isalpha() Method
Program to print ASCII Value of a character
Python program to sort words in alphabetical order
*args and **kwargs in Python
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module
Text Detection and Extraction using OpenCV and OCR
Python Random shuffle() Method
Fibonacci Series In Python | Python Program To Print
File Handling in Python
Python - XML to JSON
Python XML to Dictionary
Serialize Python dictionary to XML