- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python chr() Function
The char() function in Python returns the character corresponding to the specified Unicode. For example:
x = 65 print(chr(x)) x = 100 print(chr(x)) x = 51 print(chr(x)) x = 40 print(chr(x))
The output produced by this program is:
A d 3 (
Python chr() Function Syntax
The syntax of chr() function in Python is:
chr(x)
where x is an integer value refers to the Unicode. The value of x ranges from 0 to 1,114,111. If specified value (value of x) is out of range, then an exception is thrown by chr(). The name of that exception is ValueError.
Python chr() Function Example
Here is an example of chr() function in Python. This program receives the Unicode value from the user at run-time of the program, and prints the equivalent character corresponding to given Unicode:
print("Enter the Unicode Value: ", end="") u = int(input()) ch = chr(u) print("\nEquivalent Character =", ch)
The snapshot given below shows the sample run of above program, with user input 176 as value to find and print its corresponding character using chr() function:
The Unicode 176 corresponding to a degree (o). Here is another example that handles the ValueError exception, when raised by chr() function:
print("Enter the Unicode Value: ", end="") u = int(input()) try: ch = chr(u) print("\nEquivalent Character =", ch) except ValueError: print("\nInvalid Unicode!")
Here is its sample run with user input 1114112:
Here is the modified version of above program. This program continues its execution of getting the Unicode value from
user and printing the equivalent character, until user types exit and hits ENTER
key:
while True: print("\nEnter the Unicode Value: ", end="") u = input() if u == "exit": break else: try: u = int(u) try: ch = chr(u) print("Equivalent Character =", ch) except ValueError: print("Invalid Unicode!") except ValueError: print("Only integer value is allowed!")
The sample run of this program with some user inputs, is shown in the snapshot given below:
« Previous Function Next Function »