Python - Character code conversions


You can convert a single character to its underlying integer code (e.g., its ASCII byte value) by using ord function.

This function returns the actual binary value used to represent the character.

The chr function performs the inverse operation, taking an integer code and converting it to the character:


print( ord('s') )
print( chr(115) )

S = '5' # w  w w.ja v  a  2  s. c  o m
S = chr(ord(S) + 1) 
print( S )
S = chr(ord(S) + 1) 
print( S )

print( int('5') )
print( ord('5') - ord('0') )


Related Topic