python学习Day5-字符串操作

字符串算法

字符串加法

tang ='hello'+'python'
tang
'hellopython'

字符串乘法

tang*3
'hellopythonhellopythonhellopython'

字符串长度

len(tang)
11

字符串操作

字符串切片

tang = '1 2 3 4 5'
tang.split()
['1', '2', '3', '4', '5']
tang = '1,2,3,4,5'
tang=tang.split(',')
tang
['1', '2', '3', '4', '5']

字符串合并

tang_str = ''
tang_str.join(tang)
'12345'

字符串替换

tang = 'hello python'
tang.replace('python','java')
'hello java'
tang
'hello python'
tang1 = tang.replace('python','java')
tang1
'hello java'

- 替换的字符串,并不改变原有变量

大小写转化

tang1.upper()
'HELLO JAVA'
tang1 = 'HEllo'
tang1.lower()
'hello'

去掉多余空格

去掉左右两边空格

tang = '        hello python  '
tang.strip()
'hello python'

去掉左边空格

tang.lstrip()
'hello python  '

去掉右方空格

tang.rstrip()
'        hello python'

规则传递字符串

指定数量

'{} {} {}'.format('tang','yu','di')
'tang yu di'

指定顺序

'{2} {1} {0}'.format('tang','yu','di')
'di yu tang'

指定参数

'{tang} {yu} {di}'.format(tang=10,yu=5,di=1)
'10 5 1'

旧式%规则(不推荐)

tang = 'hello world'
a = 123.0
b = 456
result = '%s %f %d' % (tang,a,b)
result
'hello world 123.000000 456'

未完待续

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值