A string contains characters, but a computer stores every character as a numeric code. Python provides two built-in functions for moving between characters and codes:
ord()โ character โ number;chr()โ number โ character.
They are useful when a program changes characters by a mathematical rule, such as shifting a letter by several positions.
๐ข How ord() Works
ord() accepts exactly one character and returns its numeric code:
print(ord("A")) # 65
print(ord("B")) # 66
print(ord("a")) # 97
print(ord(" ")) # 32
Uppercase and lowercase letters have different codes. Transformations therefore handle the AโZ and aโz ranges separately.
Passing more than one character raises TypeError:
ord("AUTH") # error: exactly one character is required
๐ค How chr() Works
chr() performs the reverse conversion: it accepts a numeric code and returns a character.
print(chr(65)) # A
print(chr(66)) # B
print(chr(97)) # a
The functions can be connected:
letter = "C"
code = ord(letter)
restored_letter = chr(code)
print(code) # 67
print(restored_letter) # C
โก๏ธ Shifting One Letter
Increase a code by 1 to get the next uppercase letter:
letter = "C"
shifted = chr(ord(letter) + 1)
print(shifted) # D
Add 3 for a three-position shift:
letter = "C"
shift = 3
shifted = chr(ord(letter) + shift)
print(shifted) # F
Simple addition fails at the end of the alphabet: the code after Z is not A. The shift must therefore wrap around.
๐ Why % 26 Is Needed
The English alphabet contains 26 letters. Modulo % 26 keeps a position between 0 and 25:
print(25 % 26) # 25
print(26 % 26) # 0
print(27 % 26) # 1
After the final position, the sequence returns to the beginning. This makes Z shifted by 1 become A.
An uppercase transformation has four stages:
- subtract the code for
Ato get a0โ25position; - add the shift;
- apply
% 26; - add the code for
Aand convert the number withchr().
letter = "Z"
shift = 1
base = ord("A")
position = ord(letter) - base
new_position = (position + shift) % 26
shifted = chr(base + new_position)
print(shifted) # A
Lowercase letters follow the same rule with ord("a") as their base.
โ Detecting Letter Case
Characters can be checked with comparisons:
symbol = "G"
if "A" <= symbol <= "Z":
print("Uppercase Latin letter")
elif "a" <= symbol <= "z":
print("Lowercase Latin letter")
else:
print("Another character")
Spaces, digits, and punctuation are normally preserved unchanged.
๐ Building a Caesar Cipher
A Caesar cipher loops through text and shifts every letter by the same number of positions. With a shift of 3:
A โ D
C โ F
X โ A
The complete string-processing route is:
- create an empty result string;
- loop through the source text with
for; - identify the current character type;
- shift uppercase or lowercase letters relative to the correct base;
- preserve every other character;
- return the result after the loop.
A small one-letter transformation check:
symbol = "X"
shift = 3
base = ord("A")
shifted = chr((ord(symbol) - base + shift) % 26 + base)
print(shifted) # A
Decryption uses the same rule with a negative shift. Text encrypted with 3 can be restored with -3.
A Caesar cipher is useful for learning strings and algorithms, but it does not protect real passwords, tokens, or other secrets.
โ ๏ธ Common Mistakes
Passing a Whole String to ord()
Loop through the text and pass one current character to ord().
Using One Base for Both Cases
Use ord("A") for AโZ and ord("a") for aโz.
Placing return Inside the Loop
The function then stops after the first character. Return the result after the loop.
Losing Spaces
Append unchanged characters in the else branch.
๐ฏ Summary
ord(symbol)converts one character to a number;chr(code)converts a number back to a character;% 26wraps a position inside the English alphabet;- uppercase and lowercase letters use different bases;
- a Caesar cipher applies the same cyclic shift to every letter in a string.
๐ฌ Comments (0)
No comments yet
Be the first to share your opinion about this article!