Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = ' tea milk '
result = text.strip()
print(result)Top-level scope
' tea milk 'Object 2 · str'tea milk'strip() removes characters from both ends, not from the middle. With no argument it removes whitespace.
(no output)
What do you expect this program to print?
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'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = ' tea milk '
result = text.strip()
print(result)Top-level scope
' tea milk 'Object 2 · str'tea milk'The example has finished. Compare the final output with the values and references above.
tea milk
What do you expect this program to print?
text = ' tea milk '
result = text.strip()
print(result)strip() MethodThe 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.
string.strip([chars])
chars (optional) – A string specifying the set of characters to remove. If not provided, whitespace is removed.text = " Hello World! "
cleaned = text.strip()
print(cleaned)
Hello World!
text = "---Python---"
cleaned = text.strip("-")
print(cleaned)
Python
text = "xyxyLearn Pythonxy"
result = text.strip("xy")
print(result)
Learn Python
Why? The method removed all leading and trailing x and y characters.
username = input("Enter your name: ")
username = username.strip()
print("Welcome,", username)
This ensures that any accidental spaces entered by the user are removed.
strip(): removes from both endslstrip(): removes from the left onlyrstrip(): removes from the right onlystrip() does not change the original string (strings are immutable)strip() is often used in input validation, file reading, and web scraping to clean messy strings.
strip() removes leading and trailing charactersstrip()Write a program to remove leading and trailing @ and # symbols from a string:
raw = "@@@#Python is fun##@@@"
clean = raw.strip("@#")
print(clean)
Python is fun
Connect what you noticed in the example with the idea below.
strip() removes characters from both ends, not from the middle. With no argument it removes whitespace.
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.