0Pricing
Competitive Programming Academy · Lesson

Characters, ord & chr Tricks

Convert letters to numbers and back.

Characters, ord & chr Tricks is a free Competitive Programming Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Competitive Programming Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Letters Are Numbers

Every character your program reads is stored as a number under the hood. Knowing that code point lets you do math on letters. 🔢

Meet ord()

The ord() function turns a single character into its integer code point. It is your bridge from letters into arithmetic.

print(ord('A'))  # 65
print(ord('a'))  # 97

Meet chr()

The chr() function is the reverse: give it a number and it hands back the matching character. Together they form a clean round trip.

print(chr(65))  # 'A'
print(chr(97))  # 'a'

The Letters Are Consecutive

The big trick: a to z have consecutive code points, and so do A to Z. That means you can index letters with simple subtraction.

Map a Letter to 0-25

Subtract the base letter to get a clean 0-based index. This is how you turn a letter into an array slot for counting.

idx = ord('c') - ord('a')
print(idx)  # 2

Index Back to a Letter

Going the other way, add the index to the base and call chr(). Now slot 0 becomes 'a' and slot 25 becomes 'z'.

ch = chr(ord('a') + 2)
print(ch)  # 'c'

Shift a Letter (Caesar)

To shift a letter forward, add the amount then wrap with modulo 26. This is the heart of a Caesar cipher problem.

shifted = chr((ord('y') - ord('a') + 3) % 26 + ord('a'))
print(shifted)  # 'b'

Upper and Lower Differ by 32

Because of the layout, each uppercase letter sits exactly 32 below its lowercase twin. You can change case with pure arithmetic if you ever need to.

print(ord('a') - ord('A'))  # 32

Digits Are Characters Too

The character '7' is not the number 7. Subtract ord('0') to convert a digit character into its real numeric value.

d = ord('7') - ord('0')
print(d)  # 7

Quick Letter Checks

For yes or no tests, methods like isalpha() and isdigit() are cleaner than comparing code points by hand.

print('x'.isalpha())  # True
print('5'.isdigit())  # True

Why This Matters in CP

Mapping letters to indices lets you use a tiny 26-slot array instead of a dictionary. That is faster and is a staple of string problems.

Quick Check

One quick question on letter math.

Recap

You learned that ord and chr convert letters to numbers and back, and that consecutive code points let you index, shift, and check characters with simple math. 🎉

Frequently asked questions

Is the “Characters, ord & chr Tricks” lesson free?

Yes — the full text of “Characters, ord & chr Tricks” is free to read here on the web, and the Competitive Programming Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Competitive Programming Academy course, upgrade to CoddyKit PRO.

What will I learn in “Characters, ord & chr Tricks”?

Convert letters to numbers and back. You practise Competitive Programming Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Competitive Programming Academy?

No prior experience is required. Competitive Programming Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Characters, ord & chr Tricks” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Competitive Programming Academy lesson?

Yes. Every Competitive Programming Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Characters, ord & chr Tricks
  2. Count Letters with a Frequency Table
  3. Palindrome Checks Done Right
  4. Split, Strip & Rejoin Words
← Back to Competitive Programming Academy