Tired of removing prefixes and suffixes from strings with clunky if statements or replace()?
Python 3.9+ introduced two handy methods:
removeprefix(prefix): Removes the prefix from a string.
removesuffix(suffix): Removes the suffix from a string.
These methods are cleaner, more readable, and faster than traditional approaches.
⚙️ How It Works
str.removeprefix('prefix'): Removes the specified prefix from the string.
str.removesuffix('suffix'): Removes the specified suffix from the string.
If the prefix/suffix doesn’t exist, the string remains unchanged.
Python Trick: Use str.removeprefix() and str.removesuffix() for Cleaner String Manipulation!
🔍 Introduction
Tired of removing prefixes and suffixes from strings with clunky if statements or replace()?
Python 3.9+ introduced two handy methods:
removeprefix(prefix): Removes the prefix from a string.
removesuffix(suffix): Removes the suffix from a string.
These methods are cleaner, more readable, and faster than traditional approaches.
⚙️ How It Works
str.removeprefix('prefix'): Removes the specified prefix from the string.
str.removesuffix('suffix'): Removes the specified suffix from the string.
If the prefix/suffix doesn’t exist, the string remains unchanged.
💡 Example 1: Remove Prefix
python
Copy code
Remove prefix from a string
filename = 'test_file.txt'
clean_name = filename.removeprefix('test_')
print(clean_name) # Output: file.txt
Why it works: The removeprefix() method removes only the beginning of the string, not all occurrences of the prefix.
💡 Example 2: Remove Suffix
python
Copy code
Remove suffix from a string
filename = 'data_report.csv'
clean_name = filename.removesuffix('.csv')
print(clean_name) # Output: data_report
Why it works: The removesuffix() method removes the end of the string only, unlike replace(), which works on all occurrences.
💡 Example 3: Combine removeprefix() and removesuffix()
python
Copy code
Remove both prefix and suffix
url = 'https://example.com/'
clean_url = url.removeprefix('https://').removesuffix('/')
print(clean_url) # Output: example.com
Why it works: You can chain removeprefix() and removesuffix() for cleaner, multi-step transformations.
💡 Example 4: Compare Old vs New Approach
python
Copy code
Old way (clunky)
filename = 'test_file.txt'
if filename.startswith('test_'):
filename = filename[len('test_'):]
if filename.endswith('.txt'):
filename = filename[:-len('.txt')]
print(filename) # Output: file
New way (cleaner)
filename = 'test_file.txt'.removeprefix('test_').removesuffix('.txt')
print(filename) # Output: file
Why it works: The old method is harder to read and more error-prone. The new method is clearer, faster, and simpler.
Why It’s Cool
Cleaner Code: No more awkward if conditions or manual slicing.
Readability: Easy to see the logic — no “what does this slicing do?” confusion.
No Overhead: If the prefix/suffix isn’t found, the string stays the same.
Chained Operations: Chain removeprefix() and removesuffix() for multi-step transformations.
---
EBOOKS:
Python Tricks - A Collection of Tips and Techniques: https://devasservice.lemonsqueezy.com...
Mastering PyGame - A Hands-On Guide with Code Examples: https://devasservice.lemonsqueezy.com...
Python's Magic Methods: https://leanpub.com/python-magic-methods
---
BLOG AND COURSES:
My Blog: https://developer-service.blog/
My Courses: http://courses.developer-service.blog/
Digital Shop with the Source Code for all articles from the blog: https://devasservice.lemonsqueezy.com/
---
SAAS PRODUCTS:
Cloud Home Lab - Your Lab in the Cloud (Nextcloud Hosting): https://cloudhomelab.com/
Imaginator - Now supporting Flux: https://imaginator.developer-service.io/
Pod Briefly - Your Podcast Listener Companion: https://podbriefly.com/
Blog Post Generator - Generate Blog Posts with 1-click: https://blog-post-generator.developer...
---
SOCIALS:
X (Twitter): / devasservice
GitHub: https://github.com/nunombispo
YouTube: / @developerservice
LinkedIn: / nuno-bispo
Instagram: / devasservice
TikTok at: / devasservice
My website: https://developer-service.io/
---
#Python #PythonTips #CodingTricks #CleanCode #LearnPython #DevTips #CodeQuality #SoftwareDevelopment #StringManipulation #Productivity #PythonTricks