Content · Programming · Python

Python str.strip Method: Usage and Examples

str.strip(chars=None) removes whitespace from both ends of a string, or removes characters from a supplied set. It never removes matching characters from the middle. Learn with a clear example and common mistakes to.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This guide explains str.strip Method: Usage and Examples through a focused example and practical reasoning. No prior knowledge of this specific operation is required.

Understanding the Concept


str.strip(chars=None) removes whitespace from both ends of a string, or removes characters from a supplied set. It never removes matching characters from the middle.

When You Would Use This


Prepare an input value before comparing it to a known menu command.

Step-by-Step Example


raw_command = "  Save  "
normalized_command = raw_command.strip().lower()
print(normalized_command)
print("---report---".strip("-"))

How the Example Works


The normalized command is save, and the second result is report. The method returns a new string, leaving the original value unchanged.

Example Output


save
report

Try It Yourself


Clean a value surrounded by tabs, then compare its normalized form with "yes".

Common Beginner Mistakes


  • Assuming strip() removes internal spaces.
  • Using strip(".txt") to remove an exact extension.
  • Forgetting that an empty string remains empty after stripping.

Key Takeaways


  • str.strip(chars=None) removes whitespace from both ends of a string, or removes characters from a supplied set.
  • The normalized command is save, and the second result is report.
  • Avoid this mistake: Assuming strip() removes internal spaces.
Share this resourceSend this page to someone who may find it useful.