【示例1】用 "+" 進行拼接:
~~~
a = 'hello'
b = 'python'
print(a + b)
~~~
運行結果:
~~~
hellopython
~~~
* * * * *
【示例2】用format方法格式化字符串(推薦):
~~~
score = ’100‘
print('張三的成績是:{}分'.format(score))
~~~
運行結果:
~~~
張三的成績是100分
~~~
* * * * *
【小結】
有時由于需要拼接多個字符串,如果使用+拼接會顯得雜亂并且容易出錯,推薦使用格式化字符串format方法,用一對大括號占位,后面根據占位順序依次傳入要拼接的數據