python基础

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.标识符区分大小写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值