Python 字符串转 Int、Int 转字符串

在本教程中,我们将学习如何将 Python String 转换为 int 和 int 转换为 String 在 python 中. 在我们之前的教程中,我们了解了 Python 列表附件函数。

Python 字符串到 Int

如果你阅读我们的以前的教程,你可能会注意到,在某个时候,我们使用了这种转换。事实上,这在许多情况下是必要的。例如,你正在从文件中阅读一些数据,然后它将以 String 格式进行转换,你将不得不将 String 转换为 int. 现在,我们将直接进入代码。 如果你想将字符串中表示的数字转换为 int,你必须使用 int() 函数来做到这一点。

 1num = '123'  # string data
 2
 3# print the type
 4
 5print('Type of num is :', type(num))
 6
 7# convert using int()
 8
 9num = int(num)
10
11# print the type again
12
13print('Now, type of num is :', type(num))

以下代码的输出将是

1Type of num is : <class 'str'>
2Now, type of num is : <class 'int'>

Python String To Int

将 String 转换为 int 从不同的基础

如果要将要转换为 int 的字符串属于基于 10 以外的不同数位基,则可以指定转换的基数,但请记住,输出整数始终在基于 10 中,另一个需要记住的事情是,给定的基数必须在 2 到 36 之间。

 1num = '123'
 2# print the original string
 3print('The original string :', num)
 4
 5# considering '123' be in base 10, convert it to base 10
 6
 7print('Base 10 to base 10:', int(num))
 8
 9# considering '123' be in base 8, convert it to base 10
10
11print('Base 8 to base 10 :', int(num, base=8))
12
13# considering '123' be in base 6, convert it to base 10
14
15print('Base 6 to base 10 :', int(num, base=6))

The output of the following code will be Example of Python String to Int conversion

Python Convert String To Int Base

将 String 转换为 int 时的值错误

在将字符串转换为 int 时,您可能会收到 ValueError 例外 此例外发生在您要转换的字符串不代表任何数字的情况下。假设您想要将六十进制数字转换为整数,但您未在 int() 函数中通过论点 base=16。如果有任何数字不属于十进制数字系统,则会产生 ValueError 例外。

 1"""
 2    Scenario 1: The interpreter will not raise any exception but you get wrong data
 3"""
 4num = '12'  # this is a hexadecimal value
 5
 6# the variable is considered as decimal value during conversion
 7print('The value is :', int(num))
 8
 9# the variable is considered as hexadecimal value during conversion
10print('Actual value is :', int(num, base=16))
11
12"""
13    Scenario 2: The interpreter will raise ValueError exception
14"""
15
16num = '1e'  # this is a hexadecimal value
17
18# the variable is considered as hexadecimal value during conversion
19print('Actual value of \'1e\' is :', int(num, base=16))
20
21# the variable is considered as decimal value during conversion
22print('The value is :', int(num))  # this will raise exception

上面的代码的输出将是:

1The value is : 12
2Actual value is : 18
3Actual value of '1e' is : 30
4Traceback (most recent call last):
5  File "/home/imtiaz/Desktop/str2int_exception.py", line 22, in 
6    print('The value is :', int(num))  # this will raise exception
7ValueError: invalid literal for int() with base 10: '1e'

Python String To Int ValueError

Python 对 String

将 int 转换为字符串不需要任何努力或检查. 您只需使用 str() 函数来进行转换。

1hexadecimalValue = 0x1eff
2
3print('Type of hexadecimalValue :', type(hexadecimalValue))
4
5hexadecimalValue = str(hexadecimalValue)
6
7print('Type of hexadecimalValue now :', type(hexadecimalValue))

以下代码的输出将是:

1Type of hexadecimalValue : <class 'int'>
2Type of hexadecimalValue now : <class 'str'>

Python Int To String Conversion

这是关于 Python 将 String 转换为 int 和 int 转换为 string. 参考: Python 官方文档

Published At
Categories with 技术
comments powered by Disqus