当前位置:首页 > 千层云服务器 > 正文

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

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']))

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

在线客服