python基础
格式化方法
有时候想要从其他信息中构建字符串,可以用format()
age = 20
name = 'chen'
print('{0} was {1} years old when he wrote this book'.format(name,age))
print('why is {0} playing with that python' .format(name))
python从0开始计数,也可以不选用数字
age = 20
name = 'chen'
print('{} was {} years old when he wrote this book'.format(name,age))
print('why is {} playing with that python' .format(name))
最后的输出结果:
chen was 20 years old when he wrote this book
why is chen playing with that python
format方法所做的事情是将每个参数值替换格式所在的位置,可以有更加详细的方式
1.对于浮点数0.333,保留小数点后三位
print('{0:.3f}'.format(1/3))
2.使用下划线填充文,并将文字处于中间位置,字符串长度为12位
print('{0:_^12}'.format('heheda'))
3.基于关键词输出
print('{a} love {b} '.format(a='I',b='mylove'))
重复调用print会在相互独立的一行中分别打印,可以用end制定其以空白,空格等结尾
print('a',end='')
print('b',end='')
转义序列
指定某个单引号不代表这串字符串的结尾,可以通过转义序列来实现。
原始字符串
制定位经过处理的字符串,比如转义序列,可以通过在字符串前增加r或R来指定一个原始(raw)字符串。
标识命名符
1.第一个字符必须是字母列表中的字母或下划线
2.标识符可以由字母、数字、下划线组成
3.标识符区分大小写