1 hour ago · Tech · 0 comments

A few weeks ago, I learned about the removeprefix method in Python. It lets you remove a specific prefix from the beginning of a string. For example, I can use the following code to remove www. from the beginning of a domain name:"www.jamesg.blog".removeprefix("www.") If the string doesn’t contain the prefix, nothing happens; if the string does contain the prefix, the prefix is removed.I did some digging and, via a mention of the method in Stack Overflow, I learned that Python 3.9 added support for methods for removing prefixes and suffixes from strings: removeprefix and removesuffix.When I learned about removeprefix, I felt a little bit of joy. I have been using Python for years and had no idea about this method. Instead of doing the trick to measure the length of a string I want to remove, and then removing that number of characters from the beginning of a string using indexing if the string startswith the string I want to remove, I now can use a single method: removeprefix (and…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.