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 LinuxAPT, 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:


Parameter Values:

  • characters: the character you want to delete

Examples of using the Strip() Function 

1. Take a look at the below function:

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

The Output will look like this:


2. Delete space characters:

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

The output will give:


3. Another one is given here:

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

The Output will give:

Linux for people

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

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()

Related Posts