Remove leading and trailing whitespace in Python 3.10

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Python 3.10

Certainly!

In Python, you can remove leading and trailing whitespace characters from a string by using the strip() method. Here's how to do it:

# Define a string with leading/trailing whitespace
string_with_whitespace = "    This is a string with whitespace.   "

# Remove the leading/trailing whitespace
clean_string = string_with_whitespace.strip()

# Print the result
print(clean_string)

This will output:

"This is a string with whitespace."

Note that strip() removes **all** whitespace characters from the beginning and end of the string, not just spaces. This includes tabs, newlines, and any other whitespace characters.

Here's another example:

# Define a string with lots of whitespace
messy_string = "\t   \n  Lots of  \t whitespace!  \n  \t"

# Remove the whitespace
clean_string = messy_string.strip()

# Print the result
print(clean_string)

This will output:

"Lots of  \t whitespace!"

I hope this helps! Let me know if you have any other questions.