Python拼接字符串
在Python中,可以直接使用“+”号来拼接字符串
注意:数据类型全部得是字符类型。否则会报错。
#拼接字符串
str1 = '我的'
str2 = '语文成绩为:'
score = 100
print(str1 + str2 + str(score))
代码运行结果为:
=========================== RESTART: E:\python_work\python_demo.py ===========================
我的语文成绩为:100
>>>
报错示例:
#拼接字符串
str1 = '我的'
str2 = '语文成绩为:'
score = 100
print(str1 + str2 + score) #数字类型和字符串类型拼接会报错
代码运行结果:
=========================== RESTART: E:\python_work\python_demo.py ===========================
Traceback (most recent call last):
File "E:\python_work\python_demo.py", line 491, in <module>
print(str1 + str2 + score) #数字类型和字符串类型拼接会报错
TypeError: can only concatenate str (not "int") to str
>>>