Is there a Python function that will trim whitespace (spaces and tabs) from a string? So that given input " \t example string\t " becomes "example string".

Yes, in Python, you can use the strip() method to remove leading and trailing whitespace (spaces and tabs) from a string. Here's an example:

python

input_string = " \t example string\t "
trimmed_string = input_string.strip()

print(trimmed_string)

When you run this code, it will output:

c

example string

As you can see, the strip() method removes both leading and trailing whitespace from the input string, resulting in the desired "example string" output.

Comments