Python Reference Library →

Python String strip() Method – Remove Spaces from Ends

Let’s look at string strip() method – remove spaces from ends

  1. Notice

    strip() removes characters from both ends, not from the middle. With no argument it removes whitespace.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = ' tea milk '
result = text.strip()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str' tea milk 'Object 2 · str'tea milk'
This step

strip() removes characters from both ends, not from the middle. With no argument it removes whitespace.

Picture string strip() method – remove spaces from ends

strip() removes characters from both ends, not from the middle. With no argument it removes whitespace.

Image brief

Alt text
A diagram for Python String strip() Method – Remove Spaces from Ends. Instructions: text = ' tea milk ' result = text.strip() print(result). Displayed result: tea milk. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: ' tea milk '; object 2: 'tea milk'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String strip() Method – Remove Spaces from Ends". Teaching focus: strip() removes characters from both ends, not from the middle. With no argument it removes whitespace. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = ' tea milk ' result = text.strip() print(result) | v DISPLAYED RESULT tea milk FINAL BINDINGS text --> object 1 (str) result --> object 2 (str) object 1: ' tea milk ' object 2: 'tea milk' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String strip() Method – Remove Spaces from Ends. Instructions: text = ' tea milk ' result = text.strip() print(result). Displayed result: tea milk. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: ' tea milk '; object 2: 'tea milk'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = ' tea milk '
result = text.strip()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str' tea milk 'Object 2 · str'tea milk'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
tea milk

What do you expect this program to print?

What does this example display?

text = ' tea milk '
result = text.strip()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

tea milk

Follow the printed values in order. The output is: tea milk

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

strip() removes characters from both ends, not from the middle. With no argument it removes whitespace.

strip() removes characters from both ends, not from the middle. With no argument it removes whitespace.

More examples and reference

Python String strip() Method

The strip() method in Python removes any leading (spaces at the beginning) and trailing (spaces at the end) characters from a string. By default, it removes whitespace characters like spaces, tabs, and newlines.

Syntax

string.strip([chars])

Parameters:

  • chars (optional) – A string specifying the set of characters to remove. If not provided, whitespace is removed.

Returns:

  • A new string with leading and trailing characters removed.

Example 1: Remove Leading and Trailing Spaces

text = "   Hello World!   "
cleaned = text.strip()
print(cleaned)
Hello World!

Example 2: Remove Custom Characters

text = "---Python---"
cleaned = text.strip("-")
print(cleaned)
Python

Example 3: Remove Multiple Characters

text = "xyxyLearn Pythonxy"
result = text.strip("xy")
print(result)
Learn Python

Why? The method removed all leading and trailing x and y characters.

Use Case: Clean User Input

username = input("Enter your name: ")
username = username.strip()
print("Welcome,", username)

This ensures that any accidental spaces entered by the user are removed.

Difference between strip(), lstrip(), and rstrip()

  • strip(): removes from both ends
  • lstrip(): removes from the left only
  • rstrip(): removes from the right only

Common Mistakes

  • Forgetting that strip() does not change the original string (strings are immutable)
  • Thinking it removes characters *within* the string — it only affects the ends.

Interview Tip

strip() is often used in input validation, file reading, and web scraping to clean messy strings.

Summary

  • strip() removes leading and trailing characters
  • Default: removes whitespace
  • Can remove custom characters by passing a string to strip()
  • Returns a new string (original string is unchanged)

Practice Problem

Write a program to remove leading and trailing @ and # symbols from a string:

raw = "@@@#Python is fun##@@@"
clean = raw.strip("@#")
print(clean)

Expected Output:

Python is fun

What to remember

Connect what you noticed in the example with the idea below.

Remember

strip() removes characters from both ends, not from the middle. With no argument it removes whitespace.

Watch out for this idea

strip() removes every whitespace character from the entire string.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →