Python 拼接字符串的6种方法总结:
# 拼接字符串的6种方法总结:
1.自C语言的%方式
print('第 %d 种:' % 1)
url = 'this is a %s and %s'
print(url % ('dog', 'cat'))
2.format()拼接方式
print('第 %d 种:' % 2)
url = 'this is a {} and {}'
print(url.format('dog', 'cat'))
url = 'this is a {animal1} and {animal2}'
print(url.format(animal1='dog', animal2='cat'))
3.() 类似元组方式
print('第 %d 种:' % 3)
url = 'this is a dog and'
print(url, 'cat')
4.常用的+号方式,都必须是字符串类型
print('第 %d 种:' % 4)
url = 'this is a dog and '
print(url + 'cat')
5.join()拼接方式
print('第 %d 种:' % 5)
url_list = ['this is a dog ', ' cat']
print('and'.join(url_list))
6.f-string方式
print('第 %d 种:' % 6)
animal = 'cat'
url = f'this is a dog and {animal}'
print(url)
结果:
第 1 种:
this is a dog and cat
第 2 种:
this is a dog and cat
this is a dog and cat
第 3 种:
this is a dog and cat
第 4 种:
this is a dog and cat
第 5 种:
this is a dog and cat
第 6 种:
this is a dog and cat
十万签名内容......