#
#Pytyon字符串拼接的各种方式
#
#1、 %
str1="hello"
str2="world"
str="%s %s"%(str1,str2)
print(str)
#2、 +
str1="hello"
str2="world"
str=str1+str2
print(str)
#3、 f-string Python 3.6中引入了
str1="hello"
str2="world"
str=f"{str1} {str2}"
print(str)
#4、 format
str1="hello"
str2="world"
str="{} {}".format(str1,str2)
print(str)
#5、 join
arr=["hello","world"]
str=" ".join(arr)
print(str)
#6、 * 字符串copy
str1="hello world! "
str=str1*3
print(str)
#7、 \ 多行拼接
str="hello ""world""!"
print(str)
#8、() 多行拼接
str=(
"hello"
" "
"world"
"!")
print(str)
#9、 template 注意str=temp.safe_substitute(a,b)这种方式是不可行的
from string import Template
a="hello"
b="world"
temp = Template("${str1} ${str2}!")
str=temp.safe_substitute(str1=a,str2=b)
print(str)
#10、 , 只能用于print,赋值等操作会生成元组
str1="hello"
str2="world"
print(str1,str2)
#11、 直接拼接 不能变量,没有什么用
str=‘hello‘ ‘ world‘
print(str)
str=‘hello‘‘world‘
print(str)
原文:https://www.cnblogs.com/comprehension/p/11453485.html