The ord () function in Python

Python Methods and Functions

Examples :

 Input: a Output: 97 

If given a string of length one, return an integer representing the Unicode code point of the character when the argument is is a Unicode object, or a byte value if the argument is an 8-bit string. For example, ord (& # 39; a & # 39;) returns integer 97, ord (& # 39; € & # 39;) (euro sign) returns 8364. This is the inverse of chr () for 8-bit strings and unichr () for Unicode objects. If a Unicode argument is given and Python was built using Unicode UCS2, then the character code point must be in the range [0..65535] inclusive.

If the string is longer than one and a TypeError will be thrown .

The syntax can be ord ("a") or ord (& # 39; a & # 39;), both will give the same results.

# inline function returns
# an integer representing the Unicode code

value = ord ( "A" )

 
# post to & # 39; & # 39; gives the same result

value1 = ord ( 'A' )

 
# prints the Unicode value

print value, value1

Exit:

 65 65 

Exceptions

1. TypeError : Raised when line length is greater than 1.

Runtime Error:

 Traceback (most recent call last): File "/ home / f988dfe667cdc9 a8e5658464c87ccd18.py ", line 6, in value1 = ord (' AB') TypeError: ord () expected a character, but string of length 2 found 




# inline function returns
# an integer representing the Unicode code
# demonstration of the exception

  
# Raises the exception

value1 = ord ( 'AB' )

  
# prints the Unicode value

print (value1)