← All lessons

strip: remove the spaces on the edges

Sign in · Sign up

Lesson 4.4

strip removes spaces on the edges of a string — at the start and at the end:

text = "  hello  "
print(text.strip())   # hello

Spaces inside the string stay: "I learn".strip() still leaves two spaces between the words.

strip is especially often used right after input(): the user may type an extra space by accident, and for a comparison that is already a different word.

The string is set by the check. Print it without the spaces on the edges, and then in the same cleaned up form, but in uppercase.

Hint

.strip() cuts spaces and line breaks from the edges of the string.

.lstrip() cuts on the left, .rstrip() on the right. Spaces inside the string stay as they are — that's what you need to show.