学习笔记(13):零基础掌握 Python 入门到实战-深入浅出字符串(三)

立即学习:https://edu.csdn.net/course/play/26676/338776?utm_source=blogtoedu

查看对象的属性用dir函数dir()

例:查看字符串(str)的属性和方法

dir(str)

查看字符串的方法用:help(s或str.index)

str的index方法用法:

 a='my name is'

a

'my name is'

>>> a.index('e')

6

split(分割、分裂)用法

a='i love you'

a

'i love you'

>>> a.split(' ')

['i', 'love', 'you']

jion(连接)用法

lst=['i', 'love', 'you']

>>> lst

['i', 'love', 'you']

>>> ' '.join(lst)

'i love you'

---------------------------------------------

字符串格式化输出format(格式)

>符号是右对其

^符号是居中

interest='i love {0} and {1}'

>>> interest.format('python','football')

'i love python and football'

i='i love {0:^10} and {1:>10}'

>>> i.format('python','football')

'i love   python   and   football'

------------------------------------------

字符串对数字的格式化输出,例:

'he is {0:2d} years old and {1:.2f}m in height'.format(33,1.65)

'he is 33 years old and 1.65m in height'

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值