The strip() method in Python is one of the built-in functions available in the Python library. The strip() function removes specific characters from the beginning and end of the original string. Strip() removes white spaces from the beginning and end of a string by default and returns the same string without white spaces. With the assistance of examples, we will learn about the Python String strip() function in this article.
Python String Strip()
The strip() function returns a duplicate of the string that has both the leading and trailing characters removed (based on the string argument passed).
Example
message = ‘Learn Python’
# remove leading and trailing whitespaces
print(‘Message:’, message.strip())
# Output: Message: Learn Python
String strip() syntax
The strip() function has the following syntax:
string.strip([chars])
strip() Parameters
chars (optional) – a string defining the characters to be deleted from the string’s left and right sides. Based on the given, the strip() function removes characters from both the left and right sides (a string specifying the set of characters to be removed). If the chars option is not specified, all leading and following whitespaces in the string are eliminated.
strip() Return Value
strip() returns a duplicate of the string with both the leading and trailing characters removed.
Python’s String strip() function will return:
- If the characters to be deleted are not given, the original string is returned with white spaces removed from the beginning and end.
- If there are no whitespaces at the beginning or end of the string, it will be returned as is and will match the original string.
- If the characters argument is provided, and the characters provided match, the characters at the beginning and end of the string will be deleted from the original string, and the remainder of the string will be returned.
- If the characters provided do not match the beginning or end of the original string, the string will be returned as is.
Working of the strip() method
When the character in the leftmost string does not match any of the characters in the chars parameter, it stops eliminating the leading characters.
Similarly, if the character on the right of the string does not match any of the characters in the chars parameter, it stops eliminating the trailing characters.
Example: Working of the strip() method
string = ‘SLA Welcomes All’
# Whitespaces at the beginning and end are eliminated
print(string.strip())
# All whitespace> SLA characters on the string’s left and right are eliminated
print(string.strip(‘ SLA’))
# There is no space in the argument.
# There are no characters eliminated.
print(string.strip(‘stx’))
string = ‘android is good’
print(string.strip(‘an’))
Output
SLA Welcomes All
Wel
SLA Welcomes All
droid is nice
The first expression string.strip() without any parameters stripped the whitespaces from the left and right sides of the text.
- string.strip(‘SLA’) – Removes any whitespace, x, o, and e characters that before or followed the string.
- string.strip(‘stx’) – Because string contains whitespace at the beginning and end, this expression has no effect. Because x is in the midst of the string, it is not eliminated (whitespaces lead and trail the string)
- string.strip(‘an’) – Removes the string’s leading an.
Example 2: strip() on Invalid Data Type
The Python String strip() method only works on strings and will throw an error if used on any other data type such as a list, tuple, or array.
When used on a list, for example ()
mylist = [“a”, “b”, “c”, “d”]
print(mylist.strip())
The result will be an error
Output
Traceback (most recent call last):
File “teststrip.py”, line 2, in <module>
print(mylist.strip())
AttributeError: ‘list’ object has no attribute ‘strip’
Example 3: strip() Without character parameter
str1 = “Welcome to SLA!”
after_strip = str1.strip()
print(after_strip)
str2 = “Welcome to SLA!”
after_strip1 = str2.strip()
print(after_strip1)
Output
Welcome to SLA!
Welcome to SLA!
Example 4: strip() Passing character parameters
str1 = “****Welcome to SLA123!****”
after_strip = str1.strip(“*”)
print(after_strip)
str2 = “Welcome to SLA123!”
after_strip1 = str2.strip(“123!”)
print(after_strip1)
str3 = “Welcome to Guru123!”
after_strip3 = str3.strip(“to”)
print(after_strip3)
Output
Welcome to SLA123!
Welcome to SLA
Welcome to SLA123!
Why is the Python strip() method used?
These are some of the benefits of utilizing Strip function in Python.
- It is useful to delete characters at the beginning and end of the string based on the characters to be removed from the original string.
- If the specified characters do not match those in the original string, the string will be returned as is.
- If no characters are specified to be eliminated, the whitespaces at the beginning and end of the original text will be removed.
- If there is no white space at the beginning or end of the string, it will be returned as is.
Conclusion
The Python String strip() method is one of the built-in Python functions. The function will delete the characters specified from the beginning and end of the original string. This method is particularly useful for deleting whitespaces at the beginning and end of a python string, as demonstrated in the example. It is useful to delete characters at the beginning and end of the string based on the characters to be removed from the original string. Learn the Best Python Training in Chennai with IBM Certification at SLA.