Python chr() Function: Use, Syntax, and Examples

Python chr() function: In this tutorial, we will learn about the chr() function in Python with its use, syntax, parameters, returns type, and examples. By IncludeHelp Last updated : June 23, 2023

Python chr() function

The chr() function is a library function in Python, it is used to get character value from the given ASCII code (integer value), it accepts a number (that should be an ASCII code) and returns the character.

Syntax

The following is the syntax of chr() function:

chr(number)

Parameter(s):

The following are the parameter(s):

  • number – An ASCII code (integer value) whose character value we want.

Return Value

The return type of chr() function is <class 'str'>, it returns character (string) value.

Python chr() Function: Example 1

# python code to demonstrate an example
# of chr() function

# number (ASCII code of A)
num = 65
print("character value of ", num, " is = ", chr(num))

num = 120
print("character value of ", num, " is = ", chr(num))

num = 190
print("character value of ", num, " is = ", chr(num))

Output

character value of  65  is =  A
character value of  120  is =  x
character value of  190  is =  ¾

Python chr() Function: Example 2

# chr() with out of range

print(chr(-10009010))
print(chr(65567567))

Output

Traceback (most recent call last):
  File "/home/main.py", line 3, in <module>
    print(chr(-10009010))
ValueError: chr() arg not in range(0x110000)


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.