In python, sometimes, we want to find the character when given an integer (generally ASCII values). And for this purpose, we have chr() in python, which takes in the integer as the value gives the character as the value. The argument that can be given to its integer parameter is in range (0- 1,114,111).
For example- The chr value of integer 65 is ‘A’; for 92, the value is ‘a.’
Syntax of chr() python
Here, ‘n’ is any value in the range of (0- 1,114,111). If we give negative number or number greater than the defined range, we will get an error saying- ValueError: chr() arg not in range(0x110000).
It takes only one parameter, and that is the number whose character value we want to find.
If we want to find the character value for integer – 65-
character = chr(65) print(character)
Some Examples of Python chr()
Printing characters from integers using chr() python-
a=78 b=102 c=60 print("character value for",a,":",chr(a)) print("character value for",b,":",chr(b)) print("character value for",c,":",chr(c))
character value for 78 : N character value for 102 : f character value for 60 : <
Print characters from a list of integers
integers=[100,34,45,65,72,12] for i in integers: print(chr(i))
Output- d " - A H
We can also do something just like this-
integers=[100,34,45,65,72,12] characters=list(map(chr,integers)) print(characters)
Output- ['d', '"', '-', 'A', 'H', '\x0c']
First 1000 characters
Let us make a program to print all the characters which have ASCII value between 60 to 90. You can play with the numbers and change these values as you like. But keep in mind the range of values chr() accepts.
for i in range(60,91): print("ASCII VALUE:",i,"Character:",chr(i))
ASCII VALUE: 60 Character: < ASCII VALUE: 61 Character: = ASCII VALUE: 62 Character: > ASCII VALUE: 63 Character: ? ASCII VALUE: 64 Character: @ ASCII VALUE: 65 Character: A ASCII VALUE: 66 Character: B ASCII VALUE: 67 Character: C ASCII VALUE: 68 Character: D ASCII VALUE: 69 Character: E ASCII VALUE: 70 Character: F ASCII VALUE: 71 Character: G ASCII VALUE: 72 Character: H ASCII VALUE: 73 Character: I ASCII VALUE: 74 Character: J ASCII VALUE: 75 Character: K ASCII VALUE: 76 Character: L ASCII VALUE: 77 Character: M ASCII VALUE: 78 Character: N ASCII VALUE: 79 Character: O ASCII VALUE: 80 Character: P ASCII VALUE: 81 Character: Q ASCII VALUE: 82 Character: R ASCII VALUE: 83 Character: S ASCII VALUE: 84 Character: T ASCII VALUE: 85 Character: U ASCII VALUE: 86 Character: V ASCII VALUE: 87 Character: W ASCII VALUE: 88 Character: X ASCII VALUE: 89 Character: Y ASCII VALUE: 90 Character: Z
When do we Get an Error
We generally get an error when we try giving value which are not possible.
# too large a value a=1234568297 print(chr(a))
Output- ValueError: chr() arg not in range(0x110000)
Output- ValueError: chr() arg not in range(0x110000)
Difference between chr() and ord() in python
The counterpart of chr() in python is ord() function. Unlike chr(), ord() takes characters and gives integer values as output.
Now let us make a program which changes the uppercase characters into lowercase using both chr() and ord().
string="THESE CHARACTERS WILL GET CONVERTED INTO LOWERCASE" new_string="" for i in string: # 'ord' is used to find the ascii value # The value of 'A' in ASCII is 65 and that of 'Z' is 90 if ord(i) >=65 and ord(i)<= 90: #'chr' used to find the character from ascii value new_string+=chr(ord(i)+32) else: new_string+=i print("Original string:",string) print("New String:",new_string)
Output- Original string: THESE CHARACTERS WILL GET CONVERTED INTO LOWERCASE New String: these characters will get converted into lowercase
Encryption and Decryption of String using chr()
Now let us use chr() and ord() in python to make a basic yet interesting program that can encrypt or decrypt a string. Encryption is the process of converting a plain text into a ciphered text. Decryption is just the opposite of that. Here, we convert back a ciphered text to plain text.
def encrypt(string): new_string="" for i in range(len(string)): # If the charcter is in upper case if (ord(string[i]))>=65 and (ord(string[i]))<=90: encrypt_code=30 new_string+=chr(ord(string[i])-encrypt_code) # If the character is in lower case elif(ord(string[i]))>=97 and (ord(string[i]))<=122: encrypt_code=60 new_string+=chr(ord(string[i])+encrypt_code) # Here we are assuming that the string consist of only letters and spaces else: new_string+=" " string=new_string return string
def decrypt(string): new_string="" for i in range(len(string)): if (ord(string[i]))>=35 and (ord(string[i]))<=60: encrypt_code=30 new_string+=chr(ord(string[i])+encrypt_code) elif(ord(string[i]))>=157 and (ord(string[i]))<=183: encrypt_code=60 new_string+=chr(ord(string[i])-encrypt_code) else: new_string+=" " string=new_string return (new_string)
encrypted_string=encrypt("Python is the best") print("encrypted string:",encrypted_string) decrypted_string=decrypt(encrypted_string) print("decrypted string:",decrypted_string)
encrypted string: 2µ°¤«ª ¥¯ °¤¡ ¡¯° decrypted string: Python is the best
We have studied almost everything about chr() in python. It is generally used for converting an ASCII value to a character. The counterpart for this is ord(), which saves a character into its ASCII value.
Try to run the programs on your side and let us know if you have any queries.