Python chr() Built in Function

The Python chr() function is used to get a string representing a character that corresponds to a Unicode code integer. For example, chr(97) returns the string 'a'. This function takes an integer argument and throws an error if it exceeds the range of 0 to 1,114,111.

Here are some examples of using the chr() function in Python:

# Get the character for ASCII code 65
print(chr(65))
# Output: A

# Get the character for Unicode code point 1200
print(chr(1200))
# Output: Ұ

# Get the characters for a list of integers
codes = [71, 101, 101, 107, 115]
chars = [chr(code) for code in codes]
print(''.join(chars))
# Output: Geeks

To print ‘Hello World’ using chr(), you can write:

print(chr(72) + chr(101) + chr(108) + chr(108) + chr(111) + chr(32) + chr(87) + chr(111) + chr(114) + chr(108) + chr(100))

To print a list of all uppercase letters using chr(), you can write:

letters = []
for i in range(65, 91):
    letters.append(chr(i))
print(letters)

To print a smiley face emoji using chr(), you can write:

print(chr(128512))

# print some ASCII characters
print(chr(65)) # A
print(chr(66)) # B
print(chr(67)) # C

# print some non-ASCII characters
print(chr(8364)) # €
print(chr(8730)) # √
print(chr(128512)) # 😀

# print a string using chr()
s = ""
for i in range(71, 77):
    s += chr(i)
print(s) # GEEKS

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python float() Built in Function

Python String encode() Method

Python String format() Method

Python Use Get Method Over Square Brackets

Python getattr() Built in Function