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) #返回字串
本文是Python基础教程,聚焦字符串相关内容。介绍了字符串格式化方法str.format(),它可接受无限参数且位置不固定,还展示了多种使用示例。此外,还提及了判断字符串开头、结尾、是否为空格,去除前后空格以及查找字串位置等常用方法。
1259

被折叠的 条评论
为什么被折叠?



