200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 字符串转换整数python_Python将字符串转换为整数

字符串转换整数python_Python将字符串转换为整数

时间:2018-12-05 00:33:33

相关推荐

字符串转换整数python_Python将字符串转换为整数

字符串转换整数python

In this tutorial you’ll see two ways to convert string to integer in python.

在本教程中,您将看到在python中将字符串转换为整数的两种方法。

As we know we don’t have to declare the datatype while declaring variables in Python. As python will assign a datatype according to our data stored in the variable. But when we’re working with GUI (graphical user interface) then the value fetched from a textbox will be a string by default and or we’re taking value from user usingraw_input()method (in Python 2.x) andinput()method (in Python 3.x), then that value will also be a string by default. To change that string into a int type variable, we can use two different methods as given below.

众所周知,在Python中声明变量时不必声明数据类型。 由于python将根据存储在变量中的数据分配数据类型。 但是,当我们使用GUI(图形用户界面)时,默认情况下从文本框获取的值将是字符串,或者我们正在使用raw_input()方法(在Python 2.x中)和input( )方法(在Python 3.x中),则默认情况下该值也将是字符串。 要将字符串更改为int类型变量,我们可以使用以下两种不同的方法。

Let’s say our program is:

假设我们的程序是:

#program to add two numbers in python 3number1 = input("enter number 1:")number2 = input("enter number 2:")sum = number1 + number2print("sum = " + sum)

Output:

输出:

enter number 1: 40

输入数字1:40

enter number 2: 50

输入数字2:50

sum = 4050

总和= 4050

As we can see in above program theinput()method returns a string that’s why the result stored in sum is 4050 (concatenation of two strings) instead of 90. To get the addition of number1 and number2 we have to convert both the numbers into int.

我们可以在上面的程序中看到一个s,input()方法返回一个字符串,这就是为什么存储在sum中的结果是4050(两个字符串的串联)而不是90的原因。要获得number1和number2的加法,我们必须转换两个数字进入int

Python将字符串转换为整数 (Python Convert String to Integer)

方法1:使用int() (Method 1: Using int())

#program to add two numbers in python 3number1 = input("enter number 1:")number2 = input("enter number 2:")sum = int(number1) + int(number2)print("sum = " , sum)

Output:

输出:

enter number 1: 40

输入数字1:40

enter number 2: 50

输入数字2:50

sum = 90

总和= 90

In above program we’re usingint()methodto convert string to int. We can also usefloat()methodto convert it into a float variable.

在上面的程序中,我们使用int()方法将字符串转换为int。 我们还可以使用float()方法将其转换为float变量。

On other hand, to convert all the string items in a list into int we can useint() methodas follows.

另一方面,要将列表中的所有字符串项都转换为int,我们可以使用int()方法,如下所示。

#convert string into intlist1 = ['1', '2', '3', '4', '5', '6']print(list1)list2 = [int(x) for x in list1]print(list2)

Output:

输出:

[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’]

['1','2','3','4','5','6']

[1, 2, 3, 4, 5, 6]

[1、2、3、4、5、6]

方法2:使用Decimal() (Method 2: Using Decimal())

#program to add two numbers in Python 3import decimalnumber1 = input("enter number 1:")number2 = input("enter number 2:")sum = decimal.Decimal(number1) + decimal.Decimal(number2)print("sum = " , sum)

Output:

输出:

enter number 1: 40

输入数字1:40

enter number 2: 50

输入数字2:50

sum = 90

总和= 90

Decimal() methodprovides certain methods to perform faster decimal floating point arithmetic using the module decimal likesqrt() methodor exp() methodbut as still we can use it to convert a string to int in python.

Decimal()方法提供某些方法来使用模块十进制执行更快的十进制浮点算术,如sqrt()方法或exp()方法,但仍然可以使用它在python中将字符串转换为int。

If you’ve any problem related to above article, let us know by commenting below.

如果您有与上述文章有关的任何问题,请在下面评论以告知我们。

翻译自: //03/python-convert-string-to-integer.html

字符串转换整数python

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。