In Python, the "ord()" function is a built-in function that returns the Unicode code point of a specified character. The Unicode code point is a unique integer value assigned to each character in the Unicode standard, which is used to represent text in many programming languages, including Python.
The "ord()" function takes a single argument, which is a character whose Unicode code point we want to find. The argument must be a string consisting of a single character. The function returns an integer representing the Unicode code point of the character.
Let's take a look at some examples to understand the usage of the "ord()" function in Python:
Example 1: Finding the Unicode code point of a single character
|
print(ord('A'))
|
In this example, we are using the "ord()" function to find the Unicode code point of the character 'A'. The function returns the integer value 65, which is the Unicode code point of the character 'A' according to the Unicode standard.
Example 2: Finding the Unicode code point of a non-ASCII character
|
print(ord('ç'))
|
In this example, we are using the "ord()" function to find the Unicode code point of the character 'ç'. The function returns the integer value 231, which is the Unicode code point of the character 'ç' according to the Unicode standard. Note that this character is not an ASCII character and requires a Unicode encoding to represent it.
Example 3: Using the "ord()" function with a loop
|
text = "Hello, World!"
for char in text:
print(ord(char))
|
In this example, we are using the "ord()" function to find the Unicode code point of each character in the string "Hello, World!". We are using a loop to iterate through each character in the string and print the Unicode code point of each character. This will produce a sequence of integers representing the Unicode code points of each character in the string.
In conclusion, the "ord()" function in Python is a useful built-in function that allows us to find the Unicode code point of a specified character. This function can be used with both ASCII and non-ASCII characters and can be used to process and manipulate text in various ways.