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) #返回字串