在本教程中,我们将学习如何在 python 中将 Python String 转换为 int 和 int 转换为 String。在我们之前的教程中,我们学习了Python List append函数。

Python 字符串转 Int

如果您阅读了我们之前的教程,您可能会注意到有时我们使用了这种转换。实际上,这在很多情况下是必要的。例如,您正在从文件中读取一些数据,那么它将是 String 格式,您必须将 String 转换为 int。现在,我们将直接进入代码。如果要将字符串中表示的数字转换为 int,则必须使用int()函数来执行此操作。请参见以下示例:

num = '123'  # string data

# print the type

print('Type of num is :', type(num))

# convert using int()

num = int(num)

# print the type again

print('Now, type of num is :', type(num))

以下代码的输出将是

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

Python 字符串转整数

Python 字符串转整数

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

如果要转换为 int 的字符串属于以 10 为底的不同数字基数,则可以指定转换的基数。但是请记住,输出整数始终以 10 为底。您需要记住的另一件事是给定的底数必须在 2 到 36 之间。请参阅以下示例以了解使用 base 参数将字符串转换为 int 的过程。

num = '123'
# print the original string
print('The original string :', num)

# considering '123' be in base 10, convert it to base 10

print('Base 10 to base 10:', int(num))

# considering '123' be in base 8, convert it to base 10

print('Base 8 to base 10 :', int(num, base=8))

# considering '123' be in base 6, convert it to base 10

print('Base 6 to base 10 :', int(num, base=6))

以下代码的输出将是Python String 到 Int 转换示例

Python 将 String 转换为 Int Base

Python 使用 Base 将字符串转换为 Int

将String转换为int时出现ValueError

从字符串转换为 int 时,您可能会得到ValueError异常。如果您要转换的字符串不代表任何数字,则会发生此异常。假设您要将十六进制数转换为整数。但是您没有在 int() 函数中传递参数 baseu003d16。如果有任何数字不属于十进制数字系统,它将引发ValueError异常。下面的示例将在将字符串转换为 int 时说明此异常。

"""
    Scenario 1: The interpreter will not raise any exception but you get wrong data
"""
num = '12'  # this is a hexadecimal value

# the variable is considered as decimal value during conversion
print('The value is :', int(num))

# the variable is considered as hexadecimal value during conversion
print('Actual value is :', int(num, base=16))

"""
    Scenario 2: The interpreter will raise ValueError exception
"""

num = '1e'  # this is a hexadecimal value

# the variable is considered as hexadecimal value during conversion
print('Actual value of \'1e\' is :', int(num, base=16))

# the variable is considered as decimal value during conversion
print('The value is :', int(num))  # this will raise exception

上述代码的输出将是:

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

Python String To Int ValueError

Python String To Int ValueError

Python int 转字符串

将 int 转换为字符串不需要任何努力或检查。您只需使用str()函数进行转换。请参阅以下示例。

hexadecimalValue = 0x1eff

print('Type of hexadecimalValue :', type(hexadecimalValue))

hexadecimalValue = str(hexadecimalValue)

print('Type of hexadecimalValue now :', type(hexadecimalValue))

以下代码的输出将是:

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

Python Int 到字符串转换

Python Int 到字符串的转换

这就是 Python 将 String 转换为 int 以及将 int 转换为 string 的全部内容。参考:Python官方文档

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐