Python chr() Function: A Practical Guide With Examples

When I build small reporting scripts, I often need to create clean separators, checkmarks, labels, or encoded text without hard-coding every character. That is where the Python chr() function becomes useful. Give it a number, and it returns the matching character.

For example, a CSV cleanup script may need to add a tab character between columns, generate alphabetical labels, or convert Unicode values from imported data into readable symbols. You do not need an extra module for any of this because chr() is a built-in Python function.

This practical reference guide shows how the Python chr() function works, where to use it, how it differs from ord(), and how to avoid common errors.

What Is the Python chr() Function?

The Python chr() function converts a valid Unicode code point, which is an integer that represents a character, into the matching string character.

Here is the basic syntax:

chr(number)

The number argument must be an integer between 0 and 1,114,111 (0x10FFFF). Python uses Unicode, a universal character standard that represents letters, numbers, punctuation, symbols, and emoji.

For example:

character = chr(65)

print(character)

Output:

A

The number 65 represents the uppercase letter A in Unicode. The function returns a one-character string, not a number.

You will commonly use chr() when working with:

  • Character codes from a file or API response
  • Alphabetical sequences such as A, B, C, and D
  • Escape characters such as tabs and line breaks
  • Unicode symbols, currency signs, and emoji
  • Text-cleaning and automation scripts

If you are new to Python built-in functions, review this guide on Python built-in functions to see other useful tools available without installing packages.

Python chr() Function Syntax

The Python chr() function accepts one required integer argument.

chr(i)

Here, i is the Unicode code point that Python converts into a character.

print(chr(66))
print(chr(97))
print(chr(49))

Output:

B
a
1

You can refer to the screenshot below to see the output.

Python chr() Function

In this example:

  • 66 becomes uppercase B
  • 97 becomes lowercase a
  • 49 becomes the character 1

Notice that chr(49) returns a string containing "1". It does not return the integer 1.

Python chr() Function Return Value

The chr() function always returns a string with exactly one character.

result = chr(36)

print(result)
print(type(result))

Output:

$
<class 'str'>

You can refer to the screenshot below to see the output.

chr() Function Python

This behavior matters when you combine values. If you need to work with numerical calculations, keep using integers. If you need a printable character, use chr().

For more help with strings, see how to print strings and variables in Python.

Python chr() Function With ASCII Characters

ASCII is an older character encoding standard that covers common English letters, digits, punctuation, and control characters. ASCII values also work with the Python chr() function because Unicode includes them.

Convert uppercase letters

The uppercase English alphabet starts at code point 65 for A and ends at 90 for Z.

for code in range(65, 91):
print(code, "=", chr(code))

Output:

65 = A
66 = B
67 = C
68 = D
69 = E
70 = F
71 = G
72 = H
73 = I
74 = J
75 = K
76 = L
77 = M
78 = N
79 = O
80 = P
81 = Q
82 = R
83 = S
84 = T
85 = U
86 = V
87 = W
88 = X
89 = Y
90 = Z

This technique works well when you need to create Excel-style column labels, report categories, or test values in a local automation script.

Convert lowercase letters

Lowercase letters start at 97 for a and end at 122 for z.

for code in range(97, 123):
print(code, "=", chr(code))

Output:

97 = a
98 = b
99 = c
100 = d
101 = e
102 = f
103 = g
104 = h
105 = i
106 = j
107 = k
108 = l
109 = m
110 = n
111 = o
112 = p
113 = q
114 = r
115 = s
116 = t
117 = u
118 = v
119 = w
120 = x
121 = y
122 = z

You can use this approach to generate a predictable sequence instead of typing each letter manually.

Convert digit character codes

Digit characters also have Unicode values. The code point for "0" is 48, while "9" is 57.

for code in range(48, 58):
print(code, "=", chr(code))

Output:

48 = 0
49 = 1
50 = 2
51 = 3
52 = 4
53 = 5
54 = 6
55 = 7
56 = 8
57 = 9

You can refer to the screenshot below to see the output.

chr() Function in Python

This is useful when you receive integer character codes from another system and need to convert them into readable text.

Pro Tip: I have found that chr() is most useful when a script receives numeric character codes from an external file, legacy system, or API. For normal text such as names and addresses, write the text directly instead of converting every character code.

Python chr() Function for Special Characters

The Python chr() function can create special characters that are difficult to see or type directly. This includes line breaks, tabs, quotation marks, and symbols.

Create a new line and tab

A newline character has code point 10, and a tab character has code point 9.

new_line = chr(10)
tab = chr(9)

report = "Employee" + tab + "Hours" + new_line
report += "Emily Carter" + tab + "40"

print(report)

Output:

Employee	Hours
Emily Carter 40

The tab creates spacing between the columns, and the newline moves the second record to the next line. This pattern is helpful when you generate plain-text reports or prepare tab-delimited content.

You can also use escape sequences such as \n and \t, but chr() helps when your script works with character codes dynamically.

Create punctuation characters

comma = chr(44)
colon = chr(58)
at_symbol = chr(64)

message = "Customer" + colon + " Olivia Smith" + comma + " Email" + colon + " olivia" + at_symbol + "example.com"

print(message)

Output:

Customer: Olivia Smith, Email: olivia@example.com

This example builds a string from Unicode values. In a real script, I would usually type punctuation directly. However, this technique helps when punctuation codes come from configuration data or a text-processing rule.

Python chr() Function for Unicode Symbols

Unicode supports far more than English characters. You can use chr() to return currency symbols, arrows, checkmarks, and emoji.

Create currency and status symbols

dollar = chr(36)
check_mark = chr(10003)
warning = chr(9888)

print("Invoice total:", dollar + "1,250.00")
print(check_mark, "Payment received")
print(warning, "Review overdue invoices")

Output:

Invoice total: $1,250.00
✓ Payment received
⚠ Review overdue invoices

This approach works well in terminal dashboards, command-line applications, and generated text reports. Your terminal, editor, and output file must support Unicode for every symbol to display correctly.

Create an emoji with chr()

success_emoji = chr(9989)
rocket_emoji = chr(128640)

print(success_emoji, "Daily sales report completed")
print(rocket_emoji, "Report sent to the operations team")

Output:

✅ Daily sales report completed
🚀 Report sent to the operations team

Emoji code points often look less familiar than letter codes, so use them carefully in production automation. A plain-text fallback message is safer when a target system has uncertain Unicode support.

Generate Alphabet Labels With Python chr()

One practical use of the Python chr() function is generating labels for a reporting script. Imagine that Michael Johnson needs category labels for five sections in a monthly sales report.

sales_sections = [
"North Region",
"South Region",
"East Region",
"West Region",
"Online Sales"
]

for index, section in enumerate(sales_sections):
label = chr(65 + index)
print(f"{label}. {section}")

Output:

A. North Region
B. South Region
C. East Region
D. West Region
E. Online Sales

The code starts at 65, which represents A. On each loop, index increases by one, so Python creates B, C, D, and E.

The enumerate() function gives you both the index and the item from the list. If you need a refresher on lists, see this guide on how to create an empty list in Python and build it step by step.

Generate lowercase labels

You can use 97 instead of 65 when you want lowercase labels.

tasks = [
"Validate the source file",
"Clean customer names",
"Export the final report"
]

for index, task in enumerate(tasks):
label = chr(97 + index)
print(f"{label}) {task}")

Output:

a) Validate the source file
b) Clean customer names
c) Export the final report

This style works nicely for substeps in a command-line report or text-based menu.

Build a Simple Character Code Converter

Here is a small, complete example that converts a list of Unicode code points into a readable message. This pattern is useful when you load character values from a database export, a text file, or a legacy integration.

character_codes = [72, 101, 108, 108, 111, 44, 32, 69, 109, 105, 108, 121, 33]

message = ""

for code in character_codes:
message += chr(code)

print(message)

Output:

Hello, Emily!

The script starts with a list of integers. Each loop converts one integer into a character and adds it to message.

For a short example, string concatenation works well. For a longer file with thousands of codes, use join() because it handles repeated string building more efficiently.

character_codes = [82, 101, 112, 111, 114, 116, 32, 114, 101, 97, 100, 121, 33]

message = "".join(chr(code) for code in character_codes)

print(message)

Output:

Report ready!

The generator expression converts each number with chr(), and join() combines the returned characters into one string.

If you regularly process raw text, this guide on how to iterate through a string in Python is a useful next step.

Pro Tip: In my experience, use "".join() for large character sequences. Repeated message += chr(code) operations create many temporary strings and can slow down a larger text-processing script.

Python chr() Function and ord() Function

The chr() function and ord() function perform opposite operations.

  • chr() converts an integer code point into a character.
  • ord() converts one character into its integer Unicode code point.
letter = "M"

code = ord(letter)
converted_letter = chr(code)

print("Character:", letter)
print("Unicode code point:", code)
print("Converted back:", converted_letter)

Output:

Character: M
Unicode code point: 77
Converted back: M

This is useful when you need to inspect a character, store its number, and later convert it back. The two functions often appear together in beginner Python exercises, text validators, and encoding-related scripts.

For more foundational function concepts, you can also learn how to define a function in Python and organize reusable automation code.

Handle chr() Errors Safely

Python raises a ValueError when you pass an integer outside the valid Unicode range. It raises a TypeError when you pass a value that is not an integer.

Here is a safe helper function for a reporting script.

def convert_code_to_character(code):
try:
return chr(code)
except TypeError:
return "Error: Enter an integer value."
except ValueError:
return "Error: Enter a Unicode value from 0 to 1114111."


test_codes = [65, 10003, -1, 2000000, "A"]

for code in test_codes:
print(f"{code}: {convert_code_to_character(code)}")

Output:

65: A
10003: ✓
-1: Error: Enter a Unicode value from 0 to 1114111.
2000000: Error: Enter a Unicode value from 0 to 1114111.
A: Error: Enter an integer value.

The try block runs the conversion. The except blocks catch an exception, which is an error that occurs while Python runs code. This prevents one bad value from stopping your whole automation process.

For a deeper look at handling several error types, read how to catch multiple exceptions in Python.

Things to Keep in Mind

  • Use integers only: The Python chr() function accepts an integer code point. Passing a string, float, list, or None raises a TypeError.
  • Stay within the Unicode range: Valid values run from 0 through 1114111. Negative values and larger values raise a ValueError.
  • Remember the return type: chr() returns a one-character string, even when that character looks like a number, such as "7".
  • Use direct characters when possible: Type normal text and punctuation directly. Use chr() when your script genuinely works with numeric character codes or dynamic Unicode values.
  • Prefer join() for long output: Use "".join() when converting many codes into one string. It performs better than repeated string concatenation.
  • Check Unicode support: Some older terminals, fonts, and exported files may not display emoji or advanced symbols correctly. Test output in the same environment where users will open it.

Frequently Asked Questions

What does chr() do in Python?

The Python chr() function converts an integer Unicode code point into its matching character. For example, chr(65) returns "A", and chr(36) returns "$".

What is the difference between chr() and ord() in Python?

chr() converts a number into a character, while ord() converts one character into its number. For example, chr(65) returns "A", while ord("A") returns 65.

Can I use chr() to create an emoji in Python?

Yes, you can pass a valid emoji Unicode code point to chr(). For example, chr(9989) returns a check mark emoji, although display results depend on your font and terminal support.

Why does chr() give a ValueError?

chr() raises a ValueError when the integer is below 0 or above 1114111. Use a try and except block if your script receives codes from user input or external data.

Does chr() return a string or an integer?

The chr() function returns a string containing one character. For example, chr(49) returns "1" as a string, not the integer 1.

Can I use chr() to generate the alphabet in Python?

Yes. Uppercase letters start with chr(65), and lowercase letters start with chr(97). Use a for loop with range() to generate a full alphabet sequence.

The Python chr() function gives you a simple way to turn Unicode numbers into usable characters, from letters and digits to tabs, symbols, and emoji. Start with basic values such as chr(65), then use join() and error handling when you process larger sets of character codes.

You May Also Like

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.