Inbuilt ord() function in Python
Input : a Output : 97
Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string. For example, ord(‘a’) returns the integer 97, ord(‘€’) (Euro sign) returns 8364. This is the inverse of chr() for 8-bit strings and of unichr() for unicode objects. If a unicode argument is given and Python was built with UCS2 Unicode, then the character’s code point must be in the range [0..65535] inclusive.
If the string length is more then one, and a TypeError will be raised.
The syntax can be ord(“a”) or ord(‘a’), both will give same results.
1. TypeError : Raised when string length is more than 1.
Runtime Error :
Traceback (most recent call last): File "/home/f988dfe667cdc9a8e5658464c87ccd18.py", line 6, in value1 = ord('AB') TypeError: ord() expected a character, but string of length 2 found
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python - Call function from another function
- Wand function() function in Python
- Returning a function from a function - Python
- wxPython - GetField() function function in wx.StatusBar
- How to write an empty function in Python - pass statement?
- Function Decorators in Python | Set 1 (Introduction)
- Vulnerability in input() function – Python 2.x
- Function Annotations in Python
- Sorted() function in Python
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Python Numbers | choice() function
- Python | askopenfile() function in Tkinter
- Python | Binding function in Tkinter
- floor() and ceil() function Python
- round() function in Python
- id() function in Python
- vars() function in Python
- Print powers using Anonymous Function in Python
- Zip function in Python to change to a new character set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manjeet_04