python String常用操作

总结一些常用的python中String的操作,以后查阅起来方便一些。

  • {} + .format() 的形式
print("Floating point {0:.2f}".format(345.6789))
   # f代表浮点型, 2代表两位小数
   # Floating point 345.67
   
import math
print("Floating point {0:10.3f}".format(math.pi))
   # 10代表宽度,表示左边的空格数加上数字位数和小数点的总宽度(即左边空格为5格)
   # Floating point      3.142
  
  
print("Sam has {1:d} red balls and {0:d} yellow balls".format(12, 31))
  # d代表整型
  # Sam has 31 red balls and 12 yellow balls
print("Sam has {red} red balls and {green} yellow balls".format(red=12, green=31))
  # Sam has 12 red balls and 31 yellow balls

print("In binary 4 is {0:b}".format(4))
  # b 代表二进制类型
  # In binary 4 is 100

  • strip() 方法一处字符串头尾指定的字符(默认为空格或换行符)or字符序列。
  • ' '.join(L) 将以字符串构成的列表L中的每个元素以’ '空格相连接

以后会补充正则表达式的内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值