In this article, we will learn about python chr() function with example. We will learn about it’s different parameter, return value, how to use it with list, set etc. with examples.
Getting Started
Python chr() function returns string representing a character whose Unicode code point is the integer i.
Syntax of chr() Function
chr(num)
Where,
num is integer whose unicode character we want to find out.
Parameter of chr() Function
chr() function takes only one argument – an integer value whose unicode character we want to get.
- Range of integer may vary from 0 to 1,1141,111 (0x10FFFF in base 16).
- It takes only one integer argument
Return value of chr() Function
Return value of chr() function is string representing unicode character corresponding to integer value we provide.
If integer, passed as argument, is outside range (from 0 to 1,1141,111) then chr() function returns ValueError.
Examples of chr() Function
Now, let’s see some examples of chr() function –
- Example 1: Using chr with Integer
- Example 2: Using chr with Integer
- Example 3: Using chr to print String Pattern
- Example 4: Using chr with List
- Example 5: Using chr with Set
- Example 6: Using chr when argument is Out of Range
- Example 7: Using chr when argument is not Integer
- Example 8: Using chr when argument is Hexadecimal Value
-
Example 1: Using chr with Integer
a = 65 print(chr(a))
Output:
A
chr() function converted 65 into it’s equivalent unicode character and returned value A.
-
Example 2: Using chr with Integer
a = 105 print(chr(a))
Output:
i
-
Example 3: Using chr to print String Pattern
Now, let’s print string – Tutorialwing using chr() function in python –
print(chr(84), chr(117), chr(116), chr(111), chr(114), chr(105), chr(97), chr(108), chr(119),chr(105), chr(110), chr(103))
Output:
Tutorialwing
-
Example 4: Using chr with List
Let’s see how to use chr() function with list in python –
numbers = [84, 117, 116, 111, 114, 105, 97, 108, 119, 105, 110, 103] for num in numbers: print(chr(num), end='')
Output:
Tutorialwing
-
Example 5: Using chr with Set
Let’s use chr() function with set in python –
numberSet = set([84, 117, 116, 111, 114, 105, 97, 108, 119, 105, 110, 103]) for num in numberSet: print(chr(num), end='')
Output:
agilnortTuw
-
Example 6: Using chr when argument is Out of Range
chr() function will return ValueError if integer provided as argument is out of range (from 0 to 1,1141,111). For example,
print(chr(-1000))
Output:
ValueError: chr() arg not in range(0x110000)
-1000 is not in range from 0 to 1,1141,111 (or 0x10FFFF in base 16). So, chr() function returned valueError.
-
Example 7: Using chr when argument is not Integer
What if argument provided is not integer ?
chr() function returns TypeError if argument is not Integer. For example,
print(chr("Hello"))
Output:
TypeError: an integer is required (got type str)
-
Example 8: Using chr when argument is Hexadecimal Value
We can also use chr() function with hexadecimal value and get expected result. For example –
print(chr(0x65))
Output:
e
That’s it on Python chr() function with example. We learnt how can we use chr() function with list, set etc.
Learn more at official doc