字符串转换整数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 using raw_input() method (in Python 2.x) and input() 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 3
number1 = input("enter number 1:")
number2 = input("enter number 2:")
sum = number1 + number2
print("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 the input() 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 3
number1 = 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 using int() method to convert string to int. We can also use float() method to 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 use int() method as follows.

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

#convert string into int
list1 = ['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 3
import decimal
number1 = 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() method provides certain methods to perform faster decimal floating point arithmetic using the module decimal like sqrt() method or exp() method but 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.

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

翻译自: https://www.thecrazyprogrammer.com/2018/03/python-convert-string-to-integer.html

字符串转换整数python

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值