zoukankan      html  css  js  c++  java
  • python ord()与chr()用法以及区别

    ord()函数主要用来返回对应字符的ascii码,chr()主要用来表示ascii码对应的字符他的输入时数字,可以用十进制,也可以用十六进制。

    例如:print ord('a)

              #97

              print chr(97)

             #a

             print chr(0x61)

            #a

    一个简单的程序来灵活运用。

    str1='asdfasdf123123'

    for i in rang(len(str1)):

         print  chr(ord(str1[i])-1)

    #以上程序主要实现对字符串str1里面所有的字符,转换成ascii码中比他们小一位的字符。

    题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

    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	 b -- %s	c--%s' % (chr(i),chr(j),chr(k))
    

      

     应用:

    可以用来生成随机验证码:

    import random
    # 1X3Y3ZX
    def make_code(size=7):
        res = ''
        for i in range(size):
            # 循环一次则得到一个随机字符(字母/数字)
            s = chr(random.randint(65, 90))
            num = str(random.randint(0, 9))
            res += random.choice([s, num])
        return res
    
    res=make_code()
    print(res)
  • 相关阅读:
    Photoshop 基础七 位图 矢量图 栅格化
    Photoshop 基础六 图层
    Warfare And Logistics UVALive
    Walk Through the Forest UVA
    Airport Express UVA
    Guess UVALive
    Play on Words UVA
    The Necklace UVA
    Food Delivery ZOJ
    Brackets Sequence POJ
  • 原文地址:https://www.cnblogs.com/sui776265233/p/9103251.html
Copyright © 2011-2022 走看看