Python chr() function with Examples

In the previous article, we have discussed Python Program for compile() Function
chr() Function in Python:

The character that represents the specified unicode is returned by the chr() function.

Syntax:

chr(number)

Parameters

number: It is an integer. A valid Unicode code point is represented by an integer.

The integer has a valid range of 0 to 1,114,111.

Return value:

Returns a character (string) with the integer as its Unicode code point.

If the integer falls outside of the range, a ValueError will be thrown.

Examples:

Example1:

Input:

Given first number = 68
Given second number = 101

Output:

The character that represents the given unicode 68 = D
The character that represents the given unicode 101 = e

Example2:

Input:

Given first number = 99
Given second number = 75

Output:

The character that represents the given unicode 99 = c
The character that represents the given unicode 75 = K

Program for chr() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Apply chr() function to the given number and store it in another variable.
  • Print the character that represents the given Unicode.
  • Similarly, do the same for the other number and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb1 = 68
# Apply chr() function to the given number and store it in another variable.
rslt_charctr1 = chr(gvn_numb1)
# Print the character that represents the given Unicode.
print("The character that represents the given unicode",
      gvn_numb1, "=", rslt_charctr1)
# Similarly, do the same for the other number and print the result.
gvn_numb2 = 101
rslt_charctr2 = chr(gvn_numb2)
print("The character that represents the given unicode",
      gvn_numb2, "=", rslt_charctr2)

Output:

The character that represents the given unicode 68 = D
The character that represents the given unicode 101 = e

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Apply chr() function to the given number and store it in another variable.
  • Print the character that represents the given Unicode.
  • Similarly, do the same for the other number and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the number as user input using the int(input()) function 
# and store it in a variable.
gvn_numb1 = int(input("Enter some random number = "))
# Apply chr() function to the given number and store it in another variable.
rslt_charctr1 = chr(gvn_numb1)
# Print the character that represents the given Unicode.
print("The character that represents the given unicode",
      gvn_numb1, "=", rslt_charctr1)
# Similarly, do the same for the other number and print the result.
gvn_numb2 = int(input("Enter some random number = "))
rslt_charctr2 = chr(gvn_numb2)
print("The character that represents the given unicode",
      gvn_numb2, "=", rslt_charctr2)

Output:

Enter some random number = 500
The character that represents the given unicode 500 = Ç´
Enter some random number = 75
The character that represents the given unicode 75 = K

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.