capypad
0 day streak
python / beginner
Snippet

Removing Surrounding Whitespace

When handling user input, strings often contain extra spaces. The .strip() method removes all leading and trailing whitespace, making the data easier to process and compare.

snippet.py
python
1
2
3
4
user_input = " python code "
clean_input = user_input.strip()
 
print(clean_input)
Breakdown
1
clean_input = user_input.strip()
Calls the strip method to remove spaces from both ends of the string.