What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Python ord() Function with Examples and Code

  • Jan 11, 2022
  • 5 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Shivali Bhadaniya
Python ord() Function with Examples and Code

 

Python is the easiest and most preferred programming language by any beginner stepping into the coding world. It is because python offers easy syntax and a huge range of in-built functions, which makes your programming very effective and efficient. That’s what every programmer looks for, isn’t it? In this article, let us study one such python in-built function named ord() along with its usage and example. So, let’s get started!

What is the ord() Function?

The python ord() function is an in-built method in python programming that returns an integer representing the Unicode character for your input string. Computer system only deals with the integers on a fundamental level. Therefore, in 1991, the Unicode Consortium organization declared standardized specifications for each character with their respective integer code. Unicode uniquely assigns each character that can be represented on the computer, whether a letter, symbol, space or emojis.

Here, the ord() function takes the single parameter, the string of length one and returns the Unicode character of that string as output. In short, you can say that the ord() function helps to convert the string character into the integer character.

Syntax of ord() function is:

ord(ch)

 

where ‘ch’ represents the string of length 1.

If you wish to apply the ord() function for multiple character strings, you have to retrieve each character from the string and use the ord() method. Remember that it is worth noting that ord() works with all Unicode characters, not just limited to numbers of letters. You can also use symbols with this method.

Python Ord() Function Example

Let us understand 2 scenarios where we can use the ord() function in python.

1) Python ord() function with one character

Suppose you are creating an application and wish to convert only a single character into an integer form to make your task efficiently. In this case, you can take the input from the user, which includes the string of length one and later convert it into Unicode character by using the ord() function. The below example shows how you can use the ord() function for one character input.

For example:

ch = input("Enter the character that you wish to convert ")
unicode_val = ord(ch)
print("The Unicode value of the ch", ch, "is", str(unicode_val))

 

Output

The Unicode value of the ch y is 121

 

As mentioned earlier, you can also use the ord() function for integers and symbols and convert them into their respective Unicode character, just like shown below:

For example:

ch = input("Enter the character that you wish to convert ")
unicode_val = ord(ch)
print("The Unicode value of the ch", ch, "is", str(unicode_val))

 

Output

Enter the character that you wish to convert }
The Unicode value of the ch } is 125

 

2) Python ord() function with multiple characters

The python ord() function takes only one character as the input by default. But what happens if you wish to pass two or more characters through the ord() method? The answer is TypeError. When we pass more than a single parameter as input, the function will return the error named TypeError, displaying that the input string has more than one parameter.

For Example:

ch = input("Enter the character that you wish to convert ")
unicode_val = ord(ch)
print("The Unicode value of the ch", ch, "is", str(unicode_val))

 

Output

TypeError: ord() expected a character, but string of length 2 found

 

Therefore, when you wish to check the Unicode character for a longer string, you can split it into a single character and pass it through the ord() function. As shown in the example below, you can use for loop to iterate through each character in the string and pass it through the ord() function. Later, the ord() function will return and print the Unicode character for the respective string character.

For example:

ch = "Favtutor"
for character in range(0, len(ch)):
	print(ord(ch[character]))

 

Output

70
97
118
116
117
116
111
114

 

Conclusion

While programming, there are many situations where you wish to check whether the string contains the alphabets or special characters. In this scenario, you can use the ord() function and check each character by its Unicode value. Therefore, it is highly recommended to use the python ord() function to compare the character and check whether it's an alphanumerical, special character, or emojis. To get more such technical knowledge, visit our blogs at Favtutor.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.