How to do calculations on alphabet in Python

The aim of this page📝 is to share how a simple pattern of “programming alphabet” combining ord() , chr() and doing some math in between

Pavol Kutaj
Feb 28, 2023

for example, given a letter, return another letter that is in the middle between ‘a’ and the given letter

  • Example: if the input is c, I want to receive the b
def mid_letter(letter):
return chr((ord(letter) - ord('a')) // 2 + ord('a'))

>>> >>> mid_letter("l")
'f'

LINKS

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com