博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符串转换整数python_Python将字符串转换为整数
阅读量:2521 次
发布时间:2019-05-11

本文共 3180 字,大约阅读时间需要 10 分钟。

字符串转换整数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 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 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 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 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 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() 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.

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

翻译自:

字符串转换整数python

转载地址:http://poggb.baihongyu.com/

你可能感兴趣的文章
在VIM中使用GDB调试 – 使用vimgdb
查看>>
python爬虫---从零开始(五)pyQuery库
查看>>
POJ2236(KB5-A)
查看>>
Centos MySQL数据库迁移详细步骤
查看>>
2初出茅庐--初级篇2.1
查看>>
新建 WinCE7.0 下的 Silverlight 工程
查看>>
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>