Python chr Function

Python chr Function with Examples

The chr() function is a built-in Python function that takes an integer as an argument and returns the corresponding Unicode character. This function is particularly useful for dealing with character encoding and can be used for a wide range of applications such as string manipulation, text processing, and web development. In this blog post, we will explore the chr() function in detail, including its syntax, arguments, return value, and usage examples.

Syntax and Arguments:

The syntax of the chr() function is as follows:

chr(i)

where i is the integer that represents the Unicode code point of the character that you want to retrieve. The function only takes a single argument, which must be an integer within the range 0 to 1,114,111.

Return Value: The chr() function returns a string containing the Unicode character represented by the integer argument.

Examples:

Here are five unique examples that demonstrate the use of the chr() function.

Example 1: Converting ASCII codes to characters

chr(65)

Output: A
This example demonstrates how the chr() function can be used to convert ASCII codes to characters. The ASCII code for the uppercase letter A is 65, so passing 65 as an argument to chr() returns the string “A”.

Example 2: Generating random characters

import random
chr(random.randint(65, 90))

Output: V # It generates different output everytime.
This example shows how the chr() function can be used in conjunction with the random module to generate a random uppercase letter. In this case, the function generates a random integer between 65 and 90, which represents the ASCII codes for the letters A to Z. The resulting integer is then passed to chr(), which returns the corresponding character.

Example 3: Creating strings from lists of integers

codes = [72, 101, 108, 108, 111]
message = ''.join([chr(code) for code in codes])
print(message)

Output: Hello

This example demonstrates how the chr() function can be used to create strings from lists of integers. In this case, we have a list of integers representing the ASCII codes for the letters in the word “Hello”. We use a list comprehension to loop through each integer in the list, pass it to chr(), and append the resulting character to a new list. Finally, we join the list of characters into a single string using the join() function.

Example 4: Encoding and decoding data

data = 'Hello, world!'
encoded = ''.join([chr(ord(c) + 3) for c in data])
print(encoded)

Output: Khoor/#zruog!

decoded = ''.join([chr(ord(c) - 3) for c in encoded])
print(decoded)

Output: Hello, world!

This example demonstrates how the chr() function can be used in conjunction with the ord() function to encode and decode data. In this case, we are using a simple Caesar cipher to shift the ASCII codes of each character in the string by three positions. We first encode the original string by looping through each character, passing it to ord() to get its ASCII code, adding 3 to the code, passing the resulting integer to chr() to get the corresponding character, and appending the character to a new list. We then join the list of characters into a single string to get the encoded message. To decode the message, we repeat the same process, but this time we subtract 3 from each ASCII code.

Example 5: Generating a range of Unicode characters

for i in range(128512, 128520):
print(chr(i))

Output: 😀😁😂😃😄😅😆😇

This example shows how the chr() function can be used to generate a range of Unicode characters. In this case, we are using the range function to loop through the integers from 128512 to 128520, which correspond to a range of emotional emojis. We use chr() to convert each integer to its corresponding Unicode character and print it to the console.
These emoticons are commonly used in text messages, social media, and other forms of digital communication. By using the chr() function with the appropriate code points, you can easily generate these and other Unicode characters in Python.

When to use Python chr() function:

The chr() function is useful when you want to convert an integer code point to its corresponding Unicode character. You can use this function to work with Unicode characters in your Python code, such as manipulating text data, performing string operations, or working with web content.

  • Working with text data: If you are working with text data that contains Unicode characters, you may need to use the chr() function to convert the code points to their corresponding characters.
  • String manipulation: If you need to manipulate strings that contain Unicode characters, you may need to use the chr() function to convert code points to characters and vice versa.
  • Web development: If you are working with web content that contains Unicode characters, you may need to use the chr() function to manipulate the content.
  • Printing Unicode characters: If you want to print Unicode characters to the console, you can use the chr() function to convert the code points to characters.
  • Working with databases: If you are working with a database that contains Unicode characters, you may need to use the chr() function to manipulate the data.

Conclusion:

The chr() function is useful when you need to work with Unicode characters in your Python code. It is a powerful function that simplifies the process of converting code points to characters, making it an essential tool for any developer working with text data.

List Of All Python Built-in Functions:

Click on the following link to view the complete list of built-in functions.

Python Built-in Functions

Leave a Comment

Your email address will not be published. Required fields are marked *