Python ord () and Chr () usage and differences

Source: Internet
Author: User
Tags chr ord

The Ord () function is mainly used to return the ASCII code of the corresponding character, and Chr () is used mainly to denote the ASCII code corresponding to the character of his input when the number, can be in decimal or hexadecimal.

Example: Print ord (' a)


Print Chr (97)


Print Chr (0x61)


A simple program to use flexibly.

str1= ' asdfasdf123123 '

For I in Rang (Len (str1)):

Print Chr (ord (Str1[i))-1)

#以上程序主要实现对字符串str1里面所有的字符, convert the characters in the ASCII code to one smaller than them.

Title: Two table tennis team to play, each out three people. Team A for A,b,c three, team B for X, Y, z three people. Have drawn lots to determine the contest list. Someone asked the team for a list of matches. A says he does not compare with X, C says he does not compare with x,z, please compile the procedure to find out the list of the three teams.

For I in Range (Ord (' X '), ord (' z ') + 1):    for J in Range (Ord (' X '), ord (' z ') + 1):        if I! = J: for            K in range (Ord (' X '), Ord (' z ') + 1):                if (i! = k) and (J! = k):                    if (i! = Ord (' x ')) and (k! = Ord (' x ')) and (k! = Ord (' z ')):                        Print ' Order is a--%s\t B--%s\tc--%s '% (Chr (i), Chr (j), Chr (k))


Python ord () and Chr () usage and differences

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.