python有两种符号可以来进行拼接。
+
第一种拼接方法
str = "a" + "b"
print(str) # ab
第二种拼接方法
str = 'a''b''c'
print(str) # abc
使用repr
方法将数字转换成字符串,然后再进行拼接。
age = 10;
name = 'zhangsan';
>>> print(name + '年龄为'+ repr(age))
zhangsan年龄为10
注意:python不能直接拼接字符串和数字,否则会报以下错误。
>>> print('zhangsan'+10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
注意: 拼接字符串和数字,只能使用+
加号进行拼接。使用字符引号与引号之间挨着的方式拼接会报错。
>>> print('admin'repr(1))
File "<stdin>", line 1
print('admin'repr(1))
^
SyntaxError: invalid syntax