将Python字符串转换为Int,将Int转换为String

In this tutorial, we will learn how to convert python String to int and int to String in python. In our previous tutorial we learned about Python List append function.

在本教程中,我们将学习如何在python中将python String转换为int以及将int转换为String。 在上一教程中,我们了解了Python List附加函数。

将Python字符串转换为Int (Python String to Int)

If you read our previous tutorials, you may notice that at some time we used this conversion. Actually, this is necessary in many cases. For example, you are reading some data from a file, then it will be in String format and you will have to convert String to an int.

如果您阅读了以前的教程,您可能会注意到有时我们使用了这种转换。 实际上,在许多情况下这是必要的。 例如,您正在从文件中读取某些数据,那么它将采用String格式,则必须将String转换为int。

Now, we will go straight to the code. If you want to convert a number that is represented in the string to int, you have to use int() function to do so. See the following example:

现在,我们将直接进入代码。 如果要将字符串中表示的数字转换为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))

The output of the following code will be

以下代码的输出将是

Type of num is : <class 'str'>
Now, type of num is : <class 'int'>
Python String To Int

Python String To Int

将Python字符串转换为Int

从不同的基础将String转换为int (Converting String to int from different base)

If the string you want to convert into int belongs to different number base other that base 10, you can specify the base for conversion. But remember that the output integer is always in base 10. Another thing you need to remember is that the given base must be in between 2 to 36. See the following example to understand the conversion of string to int with the base argument.

如果要转换为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))

The output of the following code will be

以下代码的输出将是

Python Convert String To Int Base

Python Convert String To Int With Base

Python使用基数将字符串转换为Int

将String转换为int时发生ValueError (ValueError when converting String to int)

While converting from string to int you may get ValueError exception. This exception occurs if the string you want to convert does not represent any numbers.

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

Suppose, you want to convert a hexadecimal number to an integer. But you did not pass argument base=16 in the int() function. It will raise a 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.

假设您要将十六进制数转换为整数。 但是您没有在int()函数中传递参数base = 16 。 如果有任何数字不属于十进制数字系统,它将引发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 output of the above code will be:

上面代码的输出将是:

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字符串转换为ValueError

Python int转换为String (Python int to String)

Converting an int to string requires no effort or checking. You just use str() function to do the conversion. See the following example.

将int转换为字符串无需费力或检查。 您只需使用str()函数进行转换。 请参见以下示例。

hexadecimalValue = 0x1eff

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

hexadecimalValue = str(hexadecimalValue)

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

The output of the following code will be:

以下代码的输出将是:

Type of hexadecimalValue : <class 'int'>
Type of hexadecimalValue now : <class 'str'>
Python Int To String Conversion

Python Int To String Conversion

Python Int到字符串的转换

That’s all about Python convert String to int and int to string conversion.

这就是关于Python将String转换为int以及将int转换为字符串的全部内容。

Reference: Python Official Doc

参考: Python官方文档

翻译自: https://www.journaldev.com/15068/python-string-to-int-int-to-string

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值