Python基础教程——字符串

Python基础教程——字符串

字符串格式化:str.format()
可以接受无限个参数,位置可以不按顺序。
例子:
‘{}{}’.format(‘hello’, ‘world’)
‘helloworld’
‘{} {}’.format(‘hello’, ‘world’)
‘hello world’
‘{0} {1}’.format(‘hello’, ‘world’)
‘hello world’
‘{1} {0} {1}’.format(‘hello’, ‘world’)
‘world hello world’

b=‘{}{}’.format(r’0’*lc,b) #在b字符串前面填充lc个‘0’

#设置参数
print(‘网站名:{name}, 地址:{url}’.format(name=‘百度’, url=‘www.baidu.com’))
网站名:百度, 地址:www.baidu.com
#通过字典设置参数
site = {‘name’: ‘百度’, ‘url’: ‘www.baidu.com’}
print(‘网站名: {name}, 地址:{url}’.format(**site))
网站名: 百度, 地址:www.baidu.com
#通过列表索引设置参数
list = [‘百度’, ‘www.baidu.com’]
print(‘网站名: {0[0]}, 地址:{0[1]}’.format(list))
网站名: 百度, 地址:www.baidu.com

str1.startswith(str2) #str1是否以str2开头
str1.endswith(str2) #str是否以str2结尾

str.isspace() #判断字符串是否含是空格
str.strip() #去掉字符串前后的空格
Str.index(str1) #返回字串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值