字符串的连接方法

a, b = 'hello', 'world'
print(a+b)
print(a,b)
print('hello''world')
print('%s%s'%('hello','world'))
print('{}{}'.format('hello','world'))
print("".join([a,b]))
print(f'{a}{b}')
print(a*3)
print(a.__mul__(3))
# 连接少量字符串时,推荐使用+号操作符。
# 如果对性能有较高要求,并且python版本在3.6以上,推荐使用f-string。例如,如下情况f-string可读性比+号要好很多:
# a = f'姓名:{name} 年龄:{age} 性别:{gender}'
# b = '姓名:' + name + '年龄:' + age + '性别:' + gender
# 连接大量字符串时,推荐使用 join 和 f-string 方式,选择时依然取决于你使用的 Python 版本以及对可读性的要求。
# Python之禅注:python3.6中,数据量不大的情况下 +操作甚至比join操作还快。

猜你喜欢

转载自www.cnblogs.com/alicelai1319/p/10740613.html