๐Ÿ“ Python

ord(), chr(), and Cyclic Letter Shifts in Python ๐Ÿ”

P
Author
PyLand Team
๐Ÿ“…
Published
09.08.2026
โฑ๏ธ
Reading time
3 min
๐Ÿ‘๏ธ
Views
88
๐ŸŒฑ
Level
Beginner

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:

  1. subtract the code for A to get a 0โ€“25 position;
  2. add the shift;
  3. apply % 26;
  4. add the code for A and convert the number with chr().
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:

  1. create an empty result string;
  2. loop through the source text with for;
  3. identify the current character type;
  4. shift uppercase or lowercase letters relative to the correct base;
  5. preserve every other character;
  6. 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;
  • % 26 wraps 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.

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

if __name__ == "__main__": the Python entry point

The if name == "main" guard defines a Python program's entry point: it distinguishes direct...

๐Ÿ“… 14.08.2026 ๐Ÿ‘๏ธ 43
๐Ÿ“

strip() and lower(): Preparing User Text ๐Ÿงน

A user may enter the correct word with extra spaces or different letter case. Python...

๐Ÿ“… 11.08.2026 ๐Ÿ‘๏ธ 49
๐Ÿ“

How Functions Work Together in Python ๐Ÿงฉ

A small function normally handles one clear task. A real program, however, contains several tasks:...

๐Ÿ“… 09.08.2026 ๐Ÿ‘๏ธ 77
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Python from Scratch: Build 7 Practical Projects Open course curriculum