我们再看一个实例,整型数据与字符串类型的数据进行相加:
实例
num_int =123
num_str ="456"
print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))
print(num_int+num_str)
以上实例输出结果为:
num_int 数据类型为:<class'int'>
num_str 数据类型为:<class'str'>
Traceback(most recent call last):
File"/runoob-test/test.py", line 7,in<module>
print(num_int+num_str)
TypeError: unsupported operand type(s)for+:'int'and'str'
从输出中可以看出,整型和字符串类型运算结果会报错,输出 TypeError。 Python 在这种情况下无法使用隐式转换。
但是,Python 为这些类型的情况提供了一种解决方案,称为显式转换。