Python 入门简明教程
1. 1. 与 Python 的第一次亲密接触-写给初学者 2. 2. Python 语法关键字 3. 3. Python 的注释和语法 4. 4. Python 基本数据类型 5. 5. Python 输入输出 6. 6. Python 运算符 7. 7. Python 变量 8. 8. Python 命名空间和变量生存范围 9. 9. Python 逻辑判断 10. 10. Python For 循环语法 11. 11. Python While 循环语法 12. 12. Python 循环语法中 break 与 continue的使用 13. 14. Python 三目运算符 14. 15. Python 循环语法中 pass 语句的使用 15. 16. Python 循环语法示例 16. 17. Python 函数 17. 18. Python main 函数 18. 19. Python print 函数 19. 20. Python print format 输出格式化 20. 21. Python print 输出到文件 21. 22. Python 输入 22. 23. Python import 导入模块 23. 25. Python 中递归的使用 24. 26. Python 匿名函数 25. 27. Python lambda 26. 28. Python PIP 包管理机制 27. 29. Python 命令行参数 28. 30. Python Numbers 包 29. 31. Python 随机数 30. 32. Python String 转 int 31. 34. Python 自定义异常 Exception 32. 35. Python 继承 33. 36. Python 父类 34. 38. Python 操作符重载 35. 39. Python __str__ and __repr__

32. Python String 转 int

在本教程中,我们将学习如何在python中将python字符串转换为int,并将int转换为String。在之前的教程中,我们了解到Python列表附加功能。

Python字符串到Int

如果您阅读了我们以前的教程,您可能会注意到我们在某些时候使用了这种转换。实际上,这在很多情况下是必要的。例如,您正在从一个文件中读取一些数据,然后它将是字符串格式,并且您必须将字符串转换为int。

现在,我们将直接进入代码。如果要将字符串中表示的数字转换为int,则必须使用int() function to do so. See the following example:


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字符串到Int

将字符串从不同的基转换为int

如果要转换为int的字符串属于以10为底的不同基数,则可以指定转换的基数。但请记住,输出的整数总是以10为基数。你需要记住的另一件事是给定的基数必须在2到36之间。请参阅下面的示例,以了解使用基参数将字符串转换为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将字符串转换为带基的Int

将字符串转换为int时出错

从字符串转换为int时,您可能会得到ValueError 例外. 如果要转换的字符串不代表任何数字,则会发生此异常。

假设要将十六进制数转换为整数。但你们并没有通过辩论基数=16整数()功能。它会引起ValueError exception if there is any digit that does not belong to the decimal number system. The following example will illustrate this exception while converting a string to 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字符串到Int值错误

Python int到String

将int转换成字符串不需要任何努力或检查。你只要用str() function to do the conversion. See the following example.


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到String的转换

这就是Python将字符串转换为int和int到字符串转换的全部内容。

参考文献:Python官方文档