×


Strip() Function in Python

strip() is a built-in Python function used to strip the string of the characters specified in the argument and print out the string without the specified characters.

It's a useful tool to quickly remove unwanted characters from a string or text.

Here at Ibmi Media, we shall look into how to use the strip() function in Python.


More about the Strip() Function 

The strip() function prints out the string identical to the original and removes the characters specified in the argument.

Strip() Function syntax is given below:

string.strip(characters)

Parameter Values:

  • characters: the character you want to delete


Examples of using the Strip() Function 

1. Take a look at the below function:

str = ",,,,,,....cat...,,,"
x = str.strip(",.")
print(x)

The Output will look like this:

cat


2. Delete space characters:

str = " cat "
x = str.strip()
print(x)

The output will give:

cat


3. Another one is given here:

# Don't use strip()
str1 = "Linux for people"
print(str1)
# Use strip()
str2 = "for people"
print(str1.strip(str2))

The Output will give:

Linux for people
Linux


[Need help in fixing Python function issues? We can help you. ]


Conclusion

This article covers how to use the strip() function in Python. In fact, the Python strip() method removes any spaces or specified characters at the start and end of a string. strip() returns a new string without the characters you have specified to remove.

The syntax for the strip() method is:

" TEST ".strip()