字符串转换整数python
Unlike many other programming languages out there, Python does not implicitly typecast integers (or floats) to strings when you concatenate them to strings.
与现有的许多其他编程语言不同,Python在将整数连接到字符串时不会隐式地将整数(或浮点数)类型转换为字符串。
Fortunately, Python has a handy built-in function str() which will convert the argument passed in to a string format.
幸运的是,Python有一个方便的内置函数str() ,它将把传入的参数转换为字符串格式。
在Python中将字符串转换为整数的错误方法 (The Wrong Way to Convert a String to an Integer in Python)
Programmers coming from other programming languages may attempt to do the following string concatenation, which will produce an error:
来自其他编程语言的程序员可能会尝试执行以下字符串连接,这将产生错误:
age = 18
string = "Hello, I am " + age + " years old"
You can run this code on repl.it.
您可以在repl.it上运行此代码 。
The error that shows up is:
显示的错误是:
Traceback (most recent call last):
File "python", line 3, in <module>
TypeError: must be str, not int
Here, TypeError: must be str, not int indicates that the integer must first be converted to a string before it can be concatenated.
在这里, TypeError: must be str, not int ,该整数必须先转换为字符串才能连接。
在Python中将字符串转换为整数的正确方法 (The Correct Way to Convert a String to an Integer in Python )
Here's a simple concatenation example:
这是一个简单的串联示例:
age = 18
print("Hello, I am " + str(age) + " years old")
# Output
# Hello, I am 18 years old
You can run this code on repl.it.
您可以在repl.it上运行此代码 。
Here's how to print 1 2 3 4 5 6 7 8 9 10 using a single string:
以下是使用单个字符串打印1 2 3 4 5 6 7 8 9 10方法:
result = ""
for i in range(1, 11):
result += str(i) + " "
print(result)
# Output
# 1 2 3 4 5 6 7 8 9 10
You can run the code on repl.it.
您可以在repl.it上运行代码 。
以下是上述代码的工作原理的逐行说明: (Here's a line-by-Line explanation of how the above code works:)
First of all a variable ‘result’ is assigned to an empty string. 首先,将变量“结果”分配给一个空字符串。 The for loop is being used to iterate over a list of numbers. for循环用于遍历数字列表。 This list of numbers is generated using the range function. 此数字列表是使用范围函数生成的。 so range(1,11) is going to generate a list of numbers from 1 to 10. 因此range(1,11)将生成一个从1到10的数字列表。 On each for loop iteration this ‘i’ variable is going to take up values from 1 to 10. 在每个for循环迭代中,此“ i”变量将采用从1到10的值。 On first iteration when the variable i=1,then the variable [result=result+str(i)+“(space character)”],str(i) converts the ‘i’ which is an integer value to a string value. 在第一次迭代中,当变量i = 1时,然后变量[result = result + str(i)+“(space character)”],str(i)将整数值“ i”转换为字符串值。 Since i=1, on the first iteration finally result=1. 由于i = 1,因此在第一次迭代中最终结果= 1。 And the same process goes on until i=10 and finally after the last iteration result=1 2 3 4 5 6 7 8 9 10. 直到i = 10,最后一次迭代结果= 1 2 3 4 5 6 7 8 9 10。 Therefore when we finally print the result after the for loop the output on the console is ‘1 2 3 4 5 6 7 8 9 10’. 因此,当我们最终在for循环之后打印结果时,控制台上的输出为'1 2 3 4 5 6 7 8 9 10'。
I hope you've found this helpful. Happy coding.
希望对您有所帮助。 快乐的编码。
翻译自: https://www.freecodecamp.org/news/python-string-to-int-how-to-convert-a-string-to-an-integer-in-python/
字符串转换整数python
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。