字符串轉換整數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 = 18string = "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 = 18print("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