温馨提示×

python中有哪些拼接字符串的方法

关关
176
2021-05-14 15:41:15
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

python中拼接字符串的方法有:1.直接连接;2.使用加号连接;3.使用逗号连接;4.使用%连接;5.使用format连接;6.使用f-string方式连接;7.使用join方法连接;

python中有哪些拼接字符串的方法

python中拼接字符串的方法有以下几种

1.直接连接字符串

print('heelo' 'world')

print('heelo''world')

2.使用加号连接字符串

>>> a,b = 'heelo','world'

>>> a + b

'heelo world'

3.使用逗号连接字符串

>>> a,b = 'heelo','world'

print(a,b)

>>> heelo world

4.使用%连接字符串

print('%s %s' % ('heelo','world'))

5.使用format连接字符串

print('{} {}' .format ('heelo','world'))

6.使用f-string方式连接字符串

>>> aa, bb = 'heelo','world'

>>> f'{aa}{bb}'

'heelo world'

7.使用join方法连接字符串

print('-'.join(['aa','bb','cc']))

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:Python字符串拼接的方法有哪些

0