Skip to content

Commit ad548be

Browse files
authored
Merge pull request #649 from realpython/python-strip
Add materials for python strip tutorial
2 parents b7192c3 + 02ad373 commit ad548be

13 files changed

+89
-0
lines changed

python-strip/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# How to Strip Characters From a Python String
2+
3+
This folder contains code snippets from the Real Python tutorial on [How to Strip Characters From a Python String](https://realpython.com/how-to-use-python-strip-remove-characters/).
4+
5+
## License
6+
7+
Distributed under the MIT license. See ``LICENSE`` for more information.

python-strip/all_whitespace.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spacy_text = " Hello , World ! "
2+
print(spacy_text.strip())
3+
4+
# Normalize whitespace
5+
words = spacy_text.split()
6+
print(" ".join(words))
7+
8+
# Remove all whitespace
9+
print(spacy_text.replace(" ", ""))

python-strip/clean_data.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data = [" Alice ", " Bob ", " Charlie\n", "\tDora"]
2+
cleaned_data = [name.strip() for name in data]
3+
print(cleaned_data)

python-strip/combine_methods.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
filename = " Report_2025 FINAL.pdf "
2+
3+
# Correctly chained methods
4+
print(filename.strip().lower().replace(" ", "_"))
5+
6+
# Incorrect order
7+
print(filename.lower().replace(" ", "_").strip())

python-strip/conditional_pipeline.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
user_input = input("Enter your input: ")
2+
3+
if not user_input.strip():
4+
print("Empty input detected!")
5+
else:
6+
print(f"You entered: {user_input}")

python-strip/html.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
html_tag = "<p>premium content</p>"
2+
3+
# Removes the set of the characters <, p, and >
4+
print(html_tag.strip("<p>"))
5+
6+
# Removes prefix and suffix character sequences
7+
print(html_tag.removeprefix("<p>").removesuffix("</p>"))

python-strip/lstrip_rstrip.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
filename = "mp3audio.mp3"
2+
3+
print(filename.lstrip("3pm"))
4+
print(filename.rstrip("3pm"))

python-strip/remove_prefix.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
filename = "txt_transcript.txt"
2+
3+
print(filename.strip("txt_"))
4+
print(filename.removeprefix("txt_"))

python-strip/remove_suffix.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pathlib import Path
2+
3+
filename = "txt_transcript.txt"
4+
5+
# Incorrect: .strip() doesn't remove sequences
6+
print(filename.strip("txt_"))
7+
# Correct: Use .removesuffix() for this task
8+
print(filename.removesuffix(".txt"))
9+
10+
# If the suffix isn't found, it returns the original string
11+
print(filename.removesuffix(".mp3"))
12+
13+
# Better to use pathlib.Path for file operations
14+
print(Path(filename).stem)

python-strip/review.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
review_en = "!!This product is incredible!!!"
2+
print(review_en.strip("!"))
3+
4+
review_es = "¡¡¡Este producto es increíble!!!"
5+
print(review_es.strip("¡!"))

0 commit comments

Comments
 (0)