Learn how I found over +10,000 bugs in Python code all over GitHub and how all those bugs could have been fixed by importing the Python module string and using the constants ascii_lowercase
and ascii_uppercase
.
Yesterday, or two days ago, I was sitting down at my desk and doing some Python work. Then, all of a sudden, I thought “No... It can't be... Can it?”...
Immediately, I opened the GitHub search and typed a search query... And immediately got 1,059 search results back, which means approximately 1,000 bugs.
And this was just one query. 25 search queries later, I had found over 10,000 bugs in Python code all over GitHub.
What bug am I talking about?
Misspelling the alphabet!
At the time of writing, there are over 10,000 occurrences of misspelt Latin alphabets! Isn't that a shame? Of all the intricacies and subtleties of Python, your code will be buggy because you forgot a letter?
string
constantsThe Python module string
contains many useful constants that you can use.
Here are some of them:
If you need the 26 ASCII letters in Python, consider using the constants from the module string
.
Use ascii_lowercase
for the lowercase letters, ascii_uppercase
for the uppercase letters, and ascii_letters
for the lowercase and uppercase letters combined:
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string
constants for digitsThe module string
also contains three constants that contain the digits of numbers written in the decimal, hexadecimal, and octal systems:
>>> import string
>>> string.digits
'0123456789'
>>> string.hexdigits
'0123456789abcdefABCDEF'
>>> string.octdigits
'01234567'
string
constantsJust for the sake of completeness, let me tell you that the module string
also has constants that contain all whitespace characters, all ASCII punctuation characters, and then a constant with all the ASCII characters that are printable:
>>> import string
>>> string.whitespace
' \t\n\r\x0b\x0c'
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
+35 chapters. +400 pages. Hundreds of examples. Over 30,000 readers!
My book “Pydon'ts” teaches you how to write elegant, expressive, and Pythonic code, to help you become a better developer. >>> Download it here 🐍🚀.
string
https://docs.python.org/3/library/string.html [last accessed 23-01-2023];