标签:二进制 图片 com https 函数 符号 erro bin lse
一 字符串的格式化
格式化字符串:就是按照统一的规格去输出字符串
1 format格式化函数
>>> "{} love {}.{}".format("I","FishC","com")
‘I love FishC.com‘
>>> "{0} love {1}.{2}".format("I","FishC","com")
‘I love FishC.com‘
>>> "{2} love {0}.{1}".format("I","FishC","com")
‘com love I.FishC‘
>>> "{a} love {b}.{c}".format(a="I",b="FishC",c="com")
‘I love FishC.com‘
>>> "{c} love {b}.{a}".format(a="I",b="FishC",c="com")
‘com love FishC.I‘
>>> "{0} love {b}.{c}".format("I",b="FishC",c="com")
‘I love FishC.com‘
>>> "{a} love {b}.{0}".format("I",b="FishC","com")
File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
>>> "{0:.1f}{1}".format(27.658,"GB")
‘27.7GB‘
2 格式化字符%
>>> ‘%d %s %c‘ % (89,99,108) ‘89 99 l‘ >>> ‘%d + %d = %d‘ % (2,5,2+5) ‘2 + 5 = 7‘ >>> ‘%o‘ % 109 ‘155‘ >>> ‘%x‘ % 198 ‘c6‘ >>> ‘%X‘ % 198 ‘C6‘ >>> ‘%f‘ % 27.908 ‘27.908000‘ >>> ‘%e‘ % 27.908 ‘2.790800e+01‘ >>> ‘%g‘ % 27.908 ‘27.908‘ >>> ‘%4.2f‘ % 27.908 ‘27.91‘ >>> ‘%10d‘ % 19 ‘ 19‘ >>> ‘%-10d‘ % 19 ‘19 ‘ >>> ‘%10+d‘ % 19 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: unsupported format character ‘+‘ (0x2b) at index 3 >>> ‘%+d‘ % 19 ‘+19‘ >>> ‘%+d‘ % -19 ‘-19‘
二 课后习题
0. 编写一个进制转换程序ex15.py:
"""功能:进制转换 """ q = True while q: num = input("请输入一个整数(输入Q结束程序):") if num != ‘Q‘: num = int(num) print("十进制:{}->二进制:{:b}".format(num,num)) print("十进制:%d->二进制:" % num,bin(num)) print("十进制:{}->十六进制:0x{:x}".format(num,num)) print("十进制:%d->十六进制:0x%x" % (num,num)) print("十进制:{}->八进制:0o{:o}".format(num,num)) print("十进制:%d->八进制:0o%o" % (num,num)) else: q = False
执行结果:
PS E:\3_work\4_python\2_code_python\03_LittleTurtleStudio> python ex15.py 请输入一个整数(输入Q结束程序):145 十进制:145->二进制:10010001 十进制:145->二进制: 0b10010001 十进制:145->十六进制:0x91 十进制:145->十六进制:0x91 十进制:145->八进制:0o221 十进制:145->八进制:0o221 请输入一个整数(输入Q结束程序):Q
标签:二进制 图片 com https 函数 符号 erro bin lse
原文地址:https://www.cnblogs.com/luoxun/p/13261819.html