Python String strip() Method

The Python strip() method is used to remove any leading and trailing characters from a string. By default, it removes whitespace characters (such as space, tab, newline) from both ends of the string.

For example:

text = "  Hello world!  "
result = text.strip()
print(result)

# Output: Hello world!

You can also pass an optional parameter characters to the strip() method. It specifies a set of characters that you want to remove from both ends of the string.

For example:

text = ",,,,,rrttgg.....banana....rrr"
result = text.strip(",.grt")
print(result)

# Output: banana 

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python chr() Built in Function

Python float() Built in Function

Python bool() Built in Function

Python Printing Readable Results

Python exec() Built in Function