This is the first post in a series to understand the different PEPs in python. PEP stands for Python Enhancement Proposals. They are long, very well researched documents that detail the necessity for a feature-request and the context of its use. PEPs go through different stages, before getting accepted and included as part of the Python distribution.
In this series, we are going to talk about the different PEPs. If you are a python developer, you might have heard about PEP-8, which lays down the style guidelines for python programming. We will explore some quirky PEPs in byte sized posts. This is not your usual PEP talk. Let’s get started.
PEP stands for Python Enhancement Proposals which propose an enhancement to the Python programming language.
Today we will start with a simple PEP named PEP-616. This has been accepted only recently and it will appear in Python 3.9. This PEP proposes to include two new methods to python strings called
How does it work ?
Let us say you have a string with double quoted string “We are always getting to live, but never living” and you want to remove the double quotes. The normal pythonic way to do this is,
quote = "We are always getting to live, but never living" if text.startswith('"'): text = text[1:] if text.endswith('"'): text = text[:-1]
The new way is to use the
quote = "We are always getting to live, but never living" quote.removeprefix('"').removesuffix('"')
Where can you use it ?
If you are given a filename
my_file.pdf and you want to just extract the name of the file without the extension. One of the myriad ways is to use the
filename = "my_file.pdf" new_filename = filename.remove_suffix(".pdf")
This is a very trivial use of this method. I am excited to see the different situations in which it can be used in the future. What would have been nicer is to remove a suffix which satisfies a pattern. Yes, you can use normal regex for that, but this could have served as a convenience method.
Python 3.9 stable version is not released to the public yet. If you want to try out the development version of python, you can install using
pyenv. There is a good article about pyenv on Real Python website. You can install python 3.9-dev version using
pyenv install 3.9-dev. Give it a try.
2020-04-23 15:16 +0000