| by Arround The Web | No comments

Ord function in Python

Computers use numbers to represent characters and letters. Before “Unicode”, there were many different ways to assign these numbers, but they were limited and incompatible with all languages. “Unicode” solves this by giving each character a unique number, thereby allowing programmers to use strings with diverse languages and symbols. To get the “Unicode” code value of the specific characters, the “ord()” function is used in Python.

This Python post will explain an in-depth guide on the “ord()” function using numerous examples.

What is the “ord()” Function in Python?

The “ord()” function in Python is utilized to convert/transform a character into its associated Unicode code. It then returns this Unicode code as an integer. “Unicode” is a standard that assigns unique numbers to each character in almost all the writing systems in the world.

Syntax

ord(character)

 

Parameter

The only parameter that the “ord()” function takes is “character” which needs to be converted into its corresponding Unicode code. This parameter can be any character, including special characters and symbols.

Return Value

This function returns the “Unicode” of the character passed to it as an argument. The outcome value is an integer/int indicating the “Unicode” character point.

Example 1: Using the “ord()” Function With Character

The below code is utilized to convert/transform the character into its Unicode code:

char = 'a'
print(ord(char))

 

In the above example code, the character “a” is passed to the “ord()” function and the function converts this character into its corresponding Unicode code.

Output

The above snippet verified that the Unicode character of the specified string is “97”.

Example 2: Using the “ord()” Function With a Special Character (Symbol)

The below code is used to transform a special character into its equivalent Unicode code:

char = '€'
print(ord(char))

 

In this example, pass the character “” to the “ord()” function. The function converts this special character into its appropriate Unicode code.

Output

The above output shows the Unicode code “8364” of the specified character.

Python “ord()” and “chr()” Functions

The “chr()” function is the inverse/opposite of the “ord()” function as it takes a Unicode code and retrieves the equivalent character instead.

Example 3: Using “ord() and “chr()” Functions

The following example shows how to utilize both functions in conjunction:

char = 'a'
unicode_code = ord(char)
print(unicode_code)
print(chr(unicode_code))

 

According to the above code:

  • The character “a” is submitted to the “ord()”, which converts it into its corresponding Unicode code.
  • The “chr()” function then takes the Unicode code as its argument and converts the Unicode code back into its corresponding character.

Output

In this output, the character has been converted into Unicode code and back to the character accordingly.

Conclusion

The “ord()” function in Python is utilized to convert/transform a particular character into its equivalent Unicode code. This function is straightforward to use, takes a single parameter, and retrieves an int value representing the Unicode code point of the character. On the other hand, the “chr()” function is the opposite of the “ord()” function and is utilized to transform/convert a Unicode code into its related character.

Share Button

Source: linuxhint.com

Leave a Reply