学无先后,达者为师

网站首页 编程语言 正文

python 将两个或者多个字符串进行拼接

作者:别出BUG求求了 更新时间: 2023-11-12 编程语言

概述:

Python中可以将两个或多个字符串拼接起来,组成新的字符串

1. + 加法运算符

代码:

str1 = 'hello'
print(str1+' '+'world!')

输出:

hello world!

2. join()将列表转化为字符串

代码:

str2 = ['hello','world','everyone']
print('*'.join(str2))
print(' '.join(str2))

输出:

hello*world*everyone

hello world everyone

3、format() 字符串中{}数量和format()参数个数必须一致

代码:

str3 = '{} {}!'.format('hello','world')
print(str3)

输出:

hello world!

4、%s 格式占位符

代码:

str4 = '%s,%s!'%('hello','world')
print(str4)

输出:

hello,world!

原文链接:https://blog.csdn.net/weixin_39589455/article/details/130506764

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新