字符串与数字类型转换:
int float str
coord = (3, 5)
print('X: {0[0]}; Y: {0[1]}'.format(coord)) # X: 3; Y: 5
a = {'a': 'test_a', 'b': 'test_b'}
print('X: {0[a]}; Y: {0[b]}'.format(a)) # X: test_a; Y: test_b
这个程序挺有意思可以研究研究。
print('{} {}'.format('hello','world')) # 不带字段 hello world
print('{0} {1}'.format('hello','world')) # 带数字编号 hello world
print('{0} {1} {0}'.format('hello','world')) # 打乱顺序 hello world hello
print('{1} {1} {0}'.format('hello','world')) # world world hello
print('{a} {tom} {a}'.format(tom='hello',a='world')) # 带关键字 world hello world
# 通过位置匹配
print('{0}, {1}, {2}'.format('a', 'b', 'c')) # a, b, c
print('{}, {}, {}'.format('a', 'b', 'c')) # 3.1+版本支持 a, b, c
print('{2}, {1}, {0}'.format('a', 'b', 'c')) # c, b, a
print('{2}, {1}, {0}'.format(*'abc')) # 可打乱顺序 c, b, a
print('{0}{1}{0}'.format('abra', 'cad')) # 可重复 abracadabra
print("{1},{0}".format("hello","world"))
#中间是.来衔接#{}{}用两个这个来传递格式化输出
#{1},{0}用标有序号01的代表format中的字符串的位置,从0开始。
幂 power引出pow
绝对值absolute value引出abs
round 引出四舍五入
round( x [, n] )
- x -- 数值表达式。
- n -- 数值表达式,表示从小数点位数。
print(round(3.1415))##默认是保留整数
print(round(3.1415,1))##保留一位数
其他:英文单词的积累
Replacement 替代
positional 位置的
round 圆的
absolute绝对的