Scripting

Strip() Function in Python

Strip() Function in Python

Introduction

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’s a smart way for users to do it. Now we will start introducing you to using the strip() function in Python. Hope you understand.

Example

str = ",,,,,,....cat...,,,"

x = str.strip(",.")

print(x)

Output:

cat

Definition

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

The syntax

string.strip(characters)

Parameter Values:

characters: the character you want to delete

More examples

Example 1: delete space characters

str = " cat "

x = str.strip()

print(x)

Output:

cat

Example 2:

# Don't use strip()

str1 = "Linux for people"

print(str1)

# Use strip()

str2 = "for people"

print(str1.strip(str2))

Output:

Linux for people

Linux

Conclusion

We introduced you to using the strip() function in Python.

Thank you for reading!

Similar Posts