Start learning free

LESSON 69 OF 673 · STRINGS I

String Strip / Trim in Python

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check