What is Python ord()? What is the use of ord()?
This chapter introduces the meaning and function of the ord() function in Python. Generally speaking, the ord() function is mainly used to return the ascii code of the corresponding character, chr( ) is mainly used to represent the characters corresponding to the ASCII code. When input, the numbers can be decimal or hexadecimal. That is to say, the ord() function is the paired function of the chr() function (for 8-bit ASCII strings) or the unichr() function (for Unicode objects). It takes one character (a string of length 1) as a parameter, Returns the corresponding ASCII value, or Unicode value. If the given Unicode character exceeds your Python definition range, a TypeError exception will be raised.
1 >>> ord("a") 2 97 3 >>> chr(97) 4 'a'
For example, to generate an alphabet list, we can do this:
>>> [chr(i) for i in range(97,123)] ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p','q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# 用户输入字符 c = input("请输入一个字符: ") # 用户输入ASCII码,并将输入的数字转为整型 a = int(input("请输入一个ASCII码: ")) print( c + " 的ASCII 码为", ord(c)) print( a , " 对应的字符为", chr(a))
1 请输入一个字符: a 2 请输入一个ASCII码: 101 3 a 的ASCII 码为 97 4 101 对应的字符为 e
or this:
>>> chr(65) 'A' >>> ord('a') 97 >>> unichr(12345) u'\u3039' >>> chr(12345) Traceback (most recent call last): File "<stdin>", line 1, in ? chr(12345) ValueError: chr() arg not in range(256) >>> ord(u'\ufffff') Traceback (most recent call last): File "<stdin>", line 1, in ? ord(u'\ufffff') TypeError: ord() expected a character, but string of length 2 found >>> ord(u'\u2345') 9029
The above is the detailed content of What is Python ord()? What is the use of ord()?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
