isupper函数和isdigit函数

一、Python中isupper() 方法检测字符串中所有的字母是否都为大写。

上代码:

    if i.isupper() and i != 'Z':     #判断是不是大写的情况(但是排除了“Z”的情况)

二、isdigit()函数的用法:

  1. 判断单个字符是否为数字
  2. 判断字符串中是否仅含有数字

上代码:

    if i.isdigit():   #先判断数字的情况,如果是数字,直接加在result的后面
    res.append(i)

三、ord()函数主要用来返回对应字符的ascii码;

四、chr()主要用来表示ascii码对应的字符,可以用十进制,也可以用十六进制。

目标:实现一种密码变换算法。九键手机键盘上的数字与字母的对应: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,把密码中出现的小写字母都变成九键键盘对应的数字。而密码中出现的大写字母则变成小写之后往后移一位。例外:Z 往后移是 a。

完整代码:

while True:
    try:
        str = input()
        res = ""
        for i in str:
            if i.isdigit():   #先判断数字的情况,如果是数字,直接加在result的后面
                res = res +i
            elif i.isupper() and i != 'Z':  #在判断是不是大写的情况(但是要排除“Z”的情况),如果是大#写字母,则先把大写字母转化成小写,再转成ascii码+1,在转成对应字符。
                res = res + chr(ord(i.lower()) + 1)
            elif i == 'Z':    #然后考虑“Z”的情况,Z直接映射成“a”
                res = res + 'a'
            else:
                if i in 'abc':   #最后是把密码中出现的小写字母都变成九键键盘对应的数字abc--2, def--3, #ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9
                    res = res + '2'
                elif i in 'def':
                    res = res + '3'
                elif i in 'ghi':
                    res = res + '4'
                elif i in 'jkl':
                    res = res + '5'
                elif i in 'mno':
                    res = res + '6'
                elif i in 'pqrs':
                    res = res + '7'
                elif i in 'tuv':
                    res = res + '8'
                else:
                    res = res + '9'
        print(res)   #最后输出结果。
    except: 
        break

输入:YUANzhi1987

输出:zvbo9441987

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother) abs (stdmath) acos (stdmath) any (cppbitset) append (cppstring) asctime (stddate) asin (stdmath) assert (stdother) assign (cppdeque) assign (cpplist) assign (cppstring) assign (cppvector) at (cppdeque) at (cppstring) at (cppvector) atan (stdmath) atan2 (stdmath) atexit (stdother) atof (stdstring) atoi (stdstring) atol (stdstring) back (cppdeque) back (cpplist) back (cppqueue) back (cppvector) bad (cppio) begin (cppdeque) begin (cpplist) begin (cppmap) begin (cppmultimap) begin (cppmultiset) begin (cppset) begin (cppstring) begin (cppvector) bsearch (stdother) c_str (cppstring) calloc (stdmem) capacity (cppstring) capacity (cppvector) ceil (stdmath) clear (cppdeque) clear (cppio) clear (cpplist) clear (cppmap) clear (cppmultimap) clear (cppmultiset) clear (cppset) clear (cppvector) clearerr (stdio) clock (stddate) compare (cppstring) copy (cppstring) cos (stdmath) cosh (stdmath) count (cppbitset) count (cppmap) count (cppmultimap) count (cppmultiset) count (cppset) ctime (stddate) data (cppstring) #define (preproc) difftime (stddate) div (stdmath) empty (cppdeque) empty (cpplist) empty (cppmap) empty (cppmultimap) empty (cppmultiset) empty (cpppriorityqueue) empty (cppqueue) empty (cppset) empty (cppstack) empty (cppstring) empty (cppvector) end (cppdeque) end (cpplist) end (cppmap) end (cppmultimap) end (cppmultiset) end (cppset) end (cppstring) end (cppvector) eof (cppio) equal_range (cppmap) equal_range (cppmultimap) equal_range (cppmultiset) equal_range (cppset) erase (cppdeque) erase (cpplist) erase (cppmap) erase (cppmultimap) erase (cppmultiset) erase (cppset) erase (cppstring) erase (cppvector) #error (preproc) exit (stdother) exp (stdmath) fabs (stdmath) fail (cppio)
isalnum(测试字符是否为英文或数字) isalpha (测试字符是否为英文字母) isascii(测试字符是否为ASCII码字符) iscntrl(测试字符是否为ASCII码的控制字符) isdigit(测试字符是否为阿拉伯数字) isgraphis(测试字符是否为可打印字符) islower(测试字符是否为小写字母) isprint(测试字符是否为可打印字符) isspace(测试字符是否为空格字符) ispunct(测试字符是否为标点符号或特殊符号) isupper(测试字符是否为大写英文字母) isxdigit(测试字符是否为16进制数字) atof(将字符串转换成浮点型数) atoi(将字符串转换成整型数) atol(将字符串转换成长整型数) gcvt(将浮点型数转换为字符串,取四舍五入) strtod(将字符串转换成浮点数) strtol(将字符串转换成长整型数) strtoul(将字符串转换成无符号长整型数) toascii(将整型数转换成合法的ASCII码字符) tolower(将大写字母转换成小写字母) toupper(将小写字母转换成大写字母) Calloc(配置内存空间) free(释放原先配置的内存) getpagesize(取得内存分页大小) malloc(配置内存空间) mmap(建立内存映射) munmap(解除内存映射) asctime(将时间和日期以字符串格式表示) ctime(将时间和日期以字符串格式表示) gettimeofday(取得目前的时间) gmtime(取得目前时间和日期) localtime(取得当地目前时间和日期) mktime(将时间结构数据转换成经过的秒数) settimeofday(设置目前时间) time(取得目前的时间) bcmp(比较内存内容) bcopy(拷贝内存内容) bzero(将一段内存内容全清为零) index(查找字符串中第一个出现的指定字符) memccpy(拷贝内存内容) memchr(在某一内存范围中查找一特定字符) …… …… ……
目录 字符测试篇............................................................................................................................... 8 isalnum(测试字符是否为英文或数字) ...................................................................... 8 isalpha (测试字符是否为英文字母) ......................................................................... 8 isascii(测试字符是否为ASCII码字符) ............................................................................. 9 iscntrl(测试字符是否为ASCII码的控制字符) .............................................................. 10 isdigit(测试字符是否为阿拉伯数字) ....................................................................... 10 isgraphis(测试字符是否为可打印字符) ................................................................... 10 islower(测试字符是否为小写字母) ......................................................................... 11 isprint(测试字符是否为可打印字符) ....................................................................... 11 isspace(测试字符是否为空格字符) ......................................................................... 12 ispunct(测试字符是否为标点符号或特殊符号) ..................................................... 13 isupper(测试字符是否为大写英文字母) ................................................................. 13 isxdigit(测试字符是否为16进制数字) .................................................................... 14 字符串转换篇 ......................................................................................................................... 14 atof(将字符串转换成浮点型数) .............................................................................. 14 atol(将字符串转换成长 整型数) ............................................................................. 15 gcvt(将浮点型数转换为字符串,取四舍五入) ...................................................... 16 strtod(将字符串转换成浮点数) ............................................................................... 17 strtol(将字符串转换成长整型数) ............................................................................ 17 strtoul(将字符串转换成无符号长整型数) .............................................................. 18 toascii(将整型数转换成合法的ASCII码字符) ........................................................ 18 tolower(将大写字母转换成小写字母) .......................................................................... 19 toupper(将小写字母转换成大写字母) .................................................................... 19 内存控制篇........................................................................................ 以下省略

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值