Quick Guide to the Python Ord Function
source link: https://hackernoon.com/quick-guide-to-the-python-ord-function
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Blogger @ ItsMyCode
In Python, the ord()
function accepts a single unit of character and returns the equivalent Unicode of the passed argument. In other words, the ord()
function can take a string or character and return an integer representing the Unicode of that string or character.
ord() Function in Python
The ord()
function is nothing but the inverse of the Python chr() function. In the chr()
function, we will convert the Unicode integer to the character, and in the ord()
, it will be the exact opposite in the ord ()
.
Syntax – ord(ch)
Parameters: Accepts Unicode character or a string of length 1.
Return Value : Returns an integer representing the Unicode character
Example ord() vs chr()
print(chr(97))
print(ord('a'))
Output
a
97
As you can see, the chr(97)
returns character ‘a’, and the inverse ord('a')
returns the integer 97.
ord() Function Examples
Let’s take a look at different types of examples.
print('Unicode of lower case alphabet a is ', ord('a')) # lower case alphabet
print('Unicode of bumber 5 is ', ord('5')) # Number
print('Unicode of symobol $ is ', ord('$')) # dollar
print('Unicode of upper case alphabet A is ', ord('A')) # Upper case alphabet
print('Unicode of zero is ', ord('0')) # Number Zero
Output
Unicode of lower case alphabet a is 97
Unicode of bumber 5 is 53
Unicode of symobol $ is 36
Unicode of upper case alphabet A is 65
Unicode of zero is 48
TypeError: ord() expected a character, but string of length 2 found.
If the argument passed to the ord()
function is more than 1 character, then Python will raise a TypeError: ord()
expected a character, but string of length 2 found.
print(ord('AB'))
Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\main.py", line 9, in <module>
print(ord('AB'))
TypeError: ord() expected a character, but string of length 2 found
This post first appeared on Python ord(): A Step-By-Step Guide
Recommend
-
39
Release 2 (2009 Jun 14) This is a very quick-and-dirty guide meant to get you started with the GNU Debugger, gdb , from the command line in a terminal. Often times gdb is run via an IDE, but many p...
-
53
Python’s built-in range function is a handy tool to know you need to perform an action a specific number of times. By the end of this article, you’ll: range range() Let’s...
-
4
how to pass a quick table like UnsafePointer & lt; T & gt; argument in a function advertisements I read that in order to pass a swift...
-
4
In search of the perfect shotI was struck by the beauty of Saturn.The Cassini spacecraft had spent years in increasingly daring orbits, capturing thousands of images of that enigmatic world, bef...
-
9
A Quick Guide To Making HTTP Requests Using PythonSo what's going on here?0 reactionsFirst, we saved our base URL on a variable so that we don't have to copy-paste it repeatedly, then we sent a simple get request...
-
5
ItsMyCode | In Python ord() function accepts a single unit of character and returns the equivalent Unicode of the passed argument. In other words, the ord() function can take...
-
5
Article Build your first Java serverless function using a Quarkus quick start
-
10
A quick guide to publish Python SDK Azure Bots in App Service A quick guide to publish Python SDK Azure Bots in App Service
-
5
Function URLs - Quick and Easy way to Invoke AWS Lambda Functions over HTTP A Function URL is a dedicated endpoint for your Lambda function. Learn how to enable Function URLs and build an API using .NET Lambda Function.
-
2
A Quick and Friendly Python 3 Guide For BeginnersAugust 7th 2023 New Story by
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK