为什么Python无法将整数和字符串一起打印而不进行转换?

num = 4
print(num + " other words")

在这段代码中,Python将返回一个错误,它不能像这样将整数和字符串一起打印-我知道可以用str将int转换为字符串,但是Python为什么不自动这样做呢?

斯蒂芬·舒尔茨(Stefan Schulz)

Programiz撰写

类型转换将一种数据类型(整数,字符串,浮点数等)的值转换为另一种数据类型的过程称为类型转换。Python有两种类型的类型转换。

  1. 隐式类型转换
  2. 显式类型转换

隐式类型转换在隐式类型转换中,Python自动将一种数据类型转换为另一种数据类型。此过程不需要任何用户参与。

让我们看一个示例,其中Python促进将较低数据类型(整数)转换为较高数据类型(浮点数)以避免数据丢失。

num_int = 123
num_flo = 1.23

num_new = num_int + num_flo

print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))

print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))

当我们运行上面的程序时,输出将是:

datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>

Value of num_new: 124.23
datatype of num_new: <class 'float'>

在上面的程序中

我们添加两个变量num_int和num_flo,将值存储在num_new中。我们将分别查看所有三个对象的数据类型。在输出中,我们可以看到num_int的数据类型是整数,而num_flo的数据类型是浮点型。此外,我们可以看到num_new具有float数据类型,因为Python总是将较小的数据类型转换为较大的数据类型,以避免数据丢失。

现在,让我们尝试添加一个字符串和一个整数,看看Python如何处理它。

示例2:字符串(较高)数据类型和整数(较低)数据类型的加法

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)

当我们运行上面的程序时,输出将是:

Data type of num_int: <class 'int'> 
Data type of num_str: <class 'str'> 

Traceback (most recent call last): 
  File "python", line 7, in <module> 
TypeError: unsupported operand type(s) for +: 'int' and 'str'

在上面的程序中

我们添加两个变量num_int和num_str。从输出中可以看到,我们得到了TypeError。在这种情况下,Python无法使用隐式转换。但是,Python针对此类情况提供了一种解决方案,称为“显式转换”。

显式类型转换在显式类型转换中,用户将对象的数据类型转换为所需的数据类型。我们使用诸如int(),float(),str()等预定义函数来执行显式类型转换。

这种转换类型也称为类型转换,因为用户强制转换(更改)对象的数据类型。

句法 :

<required_datatype>(expression)

可以通过将所需的数据类型函数分配给表达式来完成类型转换。

示例3:使用显式转换将字符串和整数相加

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))

当我们运行上面的程序时,输出将是:

Data type of num_int: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>

Data type of num_str after Type Casting: <class 'int'>

Sum of num_int and num_str: 579
Data type of the sum: <class 'int'>

在上面的程序中

我们添加num_str和num_int变量。我们使用int()函数将num_str从字符串(较高)转换为整数(较低)类型以执行加法。将num_str转换为整数后,Python可以添加这两个变量。我们得到num_sum值和数据类型为整数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Dart:将整数与字符串一起打印

Bash将字符串和整数一起作为多条件进行比较

从 json 一起打印整数和字符串值

如何将字符串数组转换为整数以与 Highcharts 一起使用

Bash条件字符串和整数一起

BigQuery将字符串和整数/时间戳连接在一起?

不能使字符串相等方法和将字符串转换为数字一起工作。

为什么此Golang代码无法将字符串转换为整数?

将字符组合在一起以字符串进行希伯来语编码打印

扫描字符串,然后将它们与整数一起打印

为什么Set-ADUser命令不允许将字符串与数字字符一起使用?

将正则表达式与javascript一起使用以进行货币字符串转换

为什么我的打印函数将()和“”连同语句一起打印?

Python为什么显示“ ValueError:无法将字符串转换为浮点数”?

在对字符串进行操作时,为什么将字符串转换为charArray?

在 Django 选择字段中一起使用整数和字符串

为什么PerformSegue应该使用字符串,以及如何将UIStoryboardSegue与字符串一起使用?

(SKNode)无法转换为字符串(它与旧的xCode一起使用)

是否可以将.svg文件与React Native一起使用而不进行转换?

在R中一起使用强制转换和拆分字符串函数

在Hive中将字符串与日期和时间一起转换为日期

为什么在SGX中,必须将[in]属性与enclave字符串参数一起使用

为什么char *总是与字符串的len一起传递给FUNCTION

为什么我的程序无法对整数字符串进行排序?

在将Pipework与Docker容器一起使用时,为什么我无法进行RTNETLINK操作?

为什么将字符串强制转换为整数不起作用?

将Azure Storage Emulator与Webjob一起使用的连接字符串是什么?

将字符串转换为整数并进行比较

php无法与子字符串一起使用