diff --git a/python-strip/README.md b/python-strip/README.md new file mode 100644 index 0000000000..f93c0e386b --- /dev/null +++ b/python-strip/README.md @@ -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. diff --git a/python-strip/all_whitespace.py b/python-strip/all_whitespace.py new file mode 100644 index 0000000000..0778a0be9e --- /dev/null +++ b/python-strip/all_whitespace.py @@ -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(" ", "")) diff --git a/python-strip/clean_data.py b/python-strip/clean_data.py new file mode 100644 index 0000000000..8f413297a5 --- /dev/null +++ b/python-strip/clean_data.py @@ -0,0 +1,3 @@ +data = [" Alice ", " Bob ", " Charlie\n", "\tDora"] +cleaned_data = [name.strip() for name in data] +print(cleaned_data) diff --git a/python-strip/combine_methods.py b/python-strip/combine_methods.py new file mode 100644 index 0000000000..905f034a0a --- /dev/null +++ b/python-strip/combine_methods.py @@ -0,0 +1,7 @@ +filename = " Report_2025 FINAL.pdf " + +# Correctly chained methods +print(filename.strip().lower().replace(" ", "_")) + +# Incorrect order +print(filename.lower().replace(" ", "_").strip()) diff --git a/python-strip/conditional_pipeline.py b/python-strip/conditional_pipeline.py new file mode 100644 index 0000000000..8af7039fc2 --- /dev/null +++ b/python-strip/conditional_pipeline.py @@ -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}") diff --git a/python-strip/html.py b/python-strip/html.py new file mode 100644 index 0000000000..38ebbd80e4 --- /dev/null +++ b/python-strip/html.py @@ -0,0 +1,7 @@ +html_tag = "

premium content

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

")) + +# Removes prefix and suffix character sequences +print(html_tag.removeprefix("

").removesuffix("

")) diff --git a/python-strip/lstrip_rstrip.py b/python-strip/lstrip_rstrip.py new file mode 100644 index 0000000000..fefd00470b --- /dev/null +++ b/python-strip/lstrip_rstrip.py @@ -0,0 +1,4 @@ +filename = "mp3audio.mp3" + +print(filename.lstrip("3pm")) +print(filename.rstrip("3pm")) diff --git a/python-strip/remove_prefix.py b/python-strip/remove_prefix.py new file mode 100644 index 0000000000..0534267c47 --- /dev/null +++ b/python-strip/remove_prefix.py @@ -0,0 +1,4 @@ +filename = "txt_transcript.txt" + +print(filename.strip("txt_")) +print(filename.removeprefix("txt_")) diff --git a/python-strip/remove_suffix.py b/python-strip/remove_suffix.py new file mode 100644 index 0000000000..fab134411c --- /dev/null +++ b/python-strip/remove_suffix.py @@ -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) diff --git a/python-strip/review.py b/python-strip/review.py new file mode 100644 index 0000000000..3e343c30ea --- /dev/null +++ b/python-strip/review.py @@ -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("¡!")) diff --git a/python-strip/strip_basics.py b/python-strip/strip_basics.py new file mode 100644 index 0000000000..7cc6724c6c --- /dev/null +++ b/python-strip/strip_basics.py @@ -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()) diff --git a/python-strip/uncommon_whitespace.py b/python-strip/uncommon_whitespace.py new file mode 100644 index 0000000000..c6058f8ee0 --- /dev/null +++ b/python-strip/uncommon_whitespace.py @@ -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")) diff --git a/python-strip/username.py b/python-strip/username.py new file mode 100644 index 0000000000..410c56315b --- /dev/null +++ b/python-strip/username.py @@ -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("_!"))