50 python one-liners everyone should know
5 min readMay 16, 2021
I was always amazed by how easily things can be done using python. Some of the tedious tasks can be done in a single line of code using python. I have gathered some of my favorite one-liners from python. I have listed out 50 of them below with an example for each.
1. Anagram
from collections import Counter
s1 = 'below'
s2 = 'elbow'
print('anagram') if Counter(s1) == Counter(s2) else print('not an anagram')
or we can also do this using the sorted() method like this.
print('anagram') if sorted(s1) == sorted(s2) else print('not an anagram')
2. Binary to decimal
decimal = int('1010', 2)
print(decimal) #10
3. Converting string to lower case
"Hi my name is Allwin".lower()
# 'hi my name is allwin'
"Hi my name is Allwin".casefold()
# 'hi my name is allwin'
4. Converting string to upper case
"hi my name is Allwin".upper()
# 'HI MY NAME IS ALLWIN'
5. Converting string to byte
"convert string to bytes using encode method".encode()
# b'convert string to bytes using encode method'