Yes, ord returns the ASCII value of a character

The ord function takes a single character and returns the number that represents it in the ASCII table. If you pass it the letter "A", it returns 65. If you pass it "a", it returns 97. If you pass it the number "5", it returns 53 — not the number five, but the ASCII code for the character that looks like five.

This matters because computers store text as numbers. Every character on your keyboard — letters, numbers, punctuation, spaces — has a corresponding number in the ASCII standard. The ord function is how you see that number instead of the character itself.

You will encounter ord most often when you are comparing characters, sorting text in a non-alphabetical way, or building a program that needs to know the numeric distance between two characters. It is a small function that solves a specific problem: you have a character and you need its numeric value.

Key Takeaways

  • The ord function returns the ASCII number for any single character you give it.
  • Uppercase and lowercase letters have different ASCII values — "A" is 65, "a" is 97.
  • Numbers as characters have ASCII values too — the character "5" is 53, not the number 5.
  • You use ord when you need to compare characters numerically or understand the numeric distance between them.

How ord works in practice

In Python, the syntax is straightforward: ord("A") returns 65. You pass the function a single character in quotes, and it gives you back an integer. If you try to pass it more than one character, like ord("AB"), the function will error — it only accepts one character at a time.

The ASCII table is ordered, which means the numbers follow a pattern. All uppercase letters come before lowercase letters. All letters come before numbers. Punctuation and special characters are scattered throughout. This ordering is why ord is useful: if you need to know whether one character comes before another in ASCII order, you can compare their ord values instead of memorizing the table.

For example, ord("A") is 65 and ord("Z") is 90. Every uppercase letter falls between those two numbers. If you write code that checks whether ord(character) >= 65 and ord(character) <= 90, you have just written a test for uppercase letters without naming them explicitly.

The reverse: chr converts numbers back to characters

If ord takes a character and returns a number, chr does the opposite. chr(65) returns "A". chr(97) returns "a". This pair of functions — ord and chr — lets you move back and forth between characters and their numeric values.

You will use chr less often than ord in everyday programming, but the two are complementary. If you have a number and you want to see what character it represents, chr is how you do it. If you have a character and you want its number, ord is the answer.

When you actually need ord in your code

The most common reason to use ord is when you are validating or categorizing text. A program that checks whether a character is uppercase, lowercase, a digit, or punctuation can use ord to test ranges instead of listing every character. This is faster and cleaner than writing separate conditions for each letter.

Another reason is when you need to sort or compare characters in a specific order. By default, most programming languages sort text alphabetically, but sometimes you need to sort by ASCII value instead — which may put numbers before letters, or uppercase before lowercase. Using ord lets you see exactly what order the computer will use.

Encryption and encoding schemes also use ord. If you are building a straightforward cipher that shifts each letter by a fixed number, you use ord to get the numeric value, add the shift, and then use chr to convert back to a character. This is how Caesar ciphers work at the code level.

ASCII versus Unicode: what ord actually returns

Technically, ord returns a Unicode value, not strictly ASCII. ASCII covers only the first 128 characters — the basic English letters, numbers, and punctuation. Unicode is a much larger standard that includes characters from every language, emoji, and special symbols.

For the first 128 characters, Unicode and ASCII are identical. ord("A") returns 65 in both systems. But if you use ord on an emoji or a character from another language, you get a much larger number that represents its position in the Unicode table. For most everyday programming, this distinction does not matter — you can think of ord as returning the character's numeric code, whether that code is ASCII or Unicode.

Common mistakes when using ord

The most frequent error is passing more than one character. ord("hello") will fail because ord only accepts a single character. If you have a string and you want the ord value of each character, you need to loop through the string and call ord on each character individually.

Another mistake is forgetting that ord returns a number, not a string. If you write code that expects a character and you pass it ord("A"), you will get 65, which is a number. If your code is expecting the character "A", the mismatch will cause an error downstream.

A third mistake is assuming that ord values are always in a predictable range. While ASCII characters fall between 0 and 127, Unicode characters can go much higher. If you write code that assumes ord will never return a number above 127, it will break when it encounters a character outside the ASCII range.

Frequently Asked Questions

What is the ord value of a space character?

ord(" ") returns 32. Spaces, tabs, and newlines all have their own ASCII values. This is why you can use ord to detect and count whitespace in text — spaces are characters too, just invisible ones.

Can I use ord to check if a character is a number?

Yes. The character "0" has ord value 48 and "9" has ord value 57. So ord(character) >= 48 and ord(character) <= 57 tests whether a character is a digit. This is faster than converting the character to an actual number and checking its type.

Why do uppercase and lowercase letters have different ord values?

The ASCII table assigns them separately. Uppercase letters run from 65 to 90, and lowercase letters run from 97 to 122. This is a design choice in the ASCII standard, not a limitation. It is why ord("A") and ord("a") give different results.

What happens if I use ord on a special character like @?

ord("@") returns 64. Every printable character on your keyboard has an ASCII value. Special characters, punctuation, and symbols all have their own numbers in the table. You can look up any character's value by running ord on it.

Is ord the same in every programming language?

The concept is the same — convert a character to its numeric code — but the function name and syntax vary. Python uses ord, JavaScript uses charCodeAt, and other languages have their own versions. The underlying idea is identical across all of them.