In Python, you can remove leading and trailing whitespace from a string using strip():
s.strip() # removes leading and trailing whitespace
s.lstrip() # removes only leading (left) whitespace
s.rstrip() # removes only trailing (right) whitespace
Here are some examples:
s = " hello "
print(s.strip()) # "hello"
print(s.lstrip()) # "hello "
print(s.rstrip()) # " hello"
strip() returns a new string — the original is not modified.
Assignment
Follow the Coding Tutorial and strip whitespace from s!
Hint
Use s.strip() and assign the result to result.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
