Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python-strip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# How to Strip Characters From a Python String

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/).

## License

Distributed under the MIT license. See ``LICENSE`` for more information.
9 changes: 9 additions & 0 deletions python-strip/all_whitespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spacy_text = " Hello , World ! "
print(spacy_text.strip())

# Normalize whitespace
words = spacy_text.split()
print(" ".join(words))

# Remove all whitespace
print(spacy_text.replace(" ", ""))
3 changes: 3 additions & 0 deletions python-strip/clean_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data = [" Alice ", " Bob ", " Charlie\n", "\tDora"]
cleaned_data = [name.strip() for name in data]
print(cleaned_data)
7 changes: 7 additions & 0 deletions python-strip/combine_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
filename = " Report_2025 FINAL.pdf "

# Correctly chained methods
print(filename.strip().lower().replace(" ", "_"))

# Incorrect order
print(filename.lower().replace(" ", "_").strip())
6 changes: 6 additions & 0 deletions python-strip/conditional_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
user_input = input("Enter your input: ")

if not user_input.strip():
print("Empty input detected!")
else:
print(f"You entered: {user_input}")
7 changes: 7 additions & 0 deletions python-strip/html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
html_tag = "<p>premium content</p>"

# Removes the set of the characters <, p, and >
print(html_tag.strip("<p>"))

# Removes prefix and suffix character sequences
print(html_tag.removeprefix("<p>").removesuffix("</p>"))
4 changes: 4 additions & 0 deletions python-strip/lstrip_rstrip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
filename = "mp3audio.mp3"

print(filename.lstrip("3pm"))
print(filename.rstrip("3pm"))
4 changes: 4 additions & 0 deletions python-strip/remove_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
filename = "txt_transcript.txt"

print(filename.strip("txt_"))
print(filename.removeprefix("txt_"))
14 changes: 14 additions & 0 deletions python-strip/remove_suffix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pathlib import Path

filename = "txt_transcript.txt"

# Incorrect: .strip() doesn't remove sequences
print(filename.strip("txt_"))
# Correct: Use .removesuffix() for this task
print(filename.removesuffix(".txt"))

# If the suffix isn't found, it returns the original string
print(filename.removesuffix(".mp3"))

# Better to use pathlib.Path for file operations
print(Path(filename).stem)
5 changes: 5 additions & 0 deletions python-strip/review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
review_en = "!!This product is incredible!!!"
print(review_en.strip("!"))

review_es = "¡¡¡Este producto es increíble!!!"
print(review_es.strip("¡!"))
7 changes: 7 additions & 0 deletions python-strip/strip_basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
original_string = " Hello, World! "
print(original_string.strip())

text = """\n\t This is a messy multi-line string.

\t """
print(text.strip())
10 changes: 10 additions & 0 deletions python-strip/uncommon_whitespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
text = "\u200b\u200b\u200bHello\u200b\u200b"

# Text contains a few zero width space Unicode characters
print(text)

# Default usage doesn't remove this character
print(text.strip())

# You need to remove it explicitly
print(text.strip("\u200b"))
6 changes: 6 additions & 0 deletions python-strip/username.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
submitted_username = "_!__YoJohnDoe!__!!"
cleaned_username = submitted_username.strip("!_")
print(cleaned_username)

# Order of characters doesn't matter
print(submitted_username.strip("_!"))