What does ord() function do in python?

The ord() function takes a single character as a parameter and returns an Integer(Unicode value) of the specified character. The first 128 Unicode values are the same as ASCII values. The syntax for ord() function is given below.

#syntax:
ord(character)

The following example illustrates the use of the ord() function.

print("The Unicode value of '6' is: ", ord('6'))
print("The Unicode value of 'A' is: ", ord('A'))
print("The Unicode value of '?' is: ", ord('?'))
print("The Unicode value of 'd' is: ", ord('d'))

The above code gives the output as

The Unicode value of '6' is:  54
The Unicode value of 'A' is:  65
The Unicode value of '?' is:  63
The Unicode value of 'd' is:  100

If we pass multiple characters to the ord() function instead of a single character it raises a TypeError exception.

print("The unicode value of 'dsa' is: ", ord('dsa'))

Output

print("The unicode value of 'dsa' is: ", ord('dsa'))
TypeError: ord() expected a character, but string of length 3 found
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions