将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 : 
Now, type of num is : 

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字符串转换为Int,将Int转换为String_第1张图片

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字符串转换为Int,将Int转换为String_第2张图片

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 : 
Type of hexadecimalValue now : 

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

你可能感兴趣的:(字符串,python,java,c++,编程语言)