字符串轉換整數python_將Python字符串轉換為Int:如何在Python中將字符串轉換為整數

字符串轉換整數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:)

  1. First of all a variable ‘result’ is assigned to an empty string.

    首先,將變量“結果”分配給一個空字符串。
  2. The for loop is being used to iterate over a list of numbers.

    for循環用于遍歷數字列表。
  3. This list of numbers is generated using the range function.

    此數字列表是使用范圍函數生成的。
  4. so range(1,11) is going to generate a list of numbers from 1 to 10.

    因此range(1,11)將生成一個從1到10的數字列表。
  5. On each for loop iteration this ‘i’ variable is going to take up values from 1 to 10.

    在每個for循環迭代中,此“ i”變量將采用從1到10的值。
  6. 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”轉換為字符串值。
  7. Since i=1, on the first iteration finally result=1.

    由于i = 1,因此在第一次迭代中最終結果= 1。
  8. 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。
  9. 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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391062.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391062.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391062.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

理解Java里面的必檢異常和非必檢異常

問題&#xff1a;理解Java里面的必檢異常和非必檢異常 Joshua Bloch在"Effective Java"里面說過 在可恢復的條件下和編程錯誤導致的運行時錯誤時&#xff0c;使用必檢異常&#xff08;第二版的第52頁&#xff09; 讓我們來看一下我對這個的正確理解吧 下面是我對…

使用vim打開文件的16進制形式,編輯和全文替換

1、先用vim打開文件的二進制形式&#xff0c;如果不以二進制可能會產生轉換錯誤。 vim -b file-to-open.dat 2、用xxd把文件轉換成十六進制格式 :%!xxd 現在就可以對待普通文本一樣查看和編輯二進制文件了。 3、vim 單文件替換方法 :%s/old/new/gc 全文執行替換,詢問是…

nlp自然語言處理_不要被NLP Research淹沒

nlp自然語言處理自然語言處理 (Natural Language Processing) 到底是怎么回事&#xff1f; (What is going on?) NLP is the new Computer VisionNLP是新的計算機視覺 With enormous amount go textual datasets available; giants like Google, Microsoft, Facebook etc have…

opencv 隨筆

裝環境好累&#xff0c;python3.6&#xff0c;opencv3.4 好不容易裝好了&#xff0c;結果 addweight的時候總是報錯 The operation is neither array op array (where arrays have the same size and the same number of channels), nor array op scalar, nor scalar op array …

js打開飛行模式_什么是飛行模式? 它有什么作用?什么時候應該打開它?

js打開飛行模式If youve flown on an airplane in the last decade and you have a smart phone, youve likely had to put that phone in airplane mode before the plane takes off.如果您在過去的十年中乘坐過飛機&#xff0c;并且擁有一部智能手機&#xff0c;那么您可能必…

在Java 里面怎么比較字符串

問題&#xff1a;在Java 里面怎么比較字符串 到目前為止&#xff0c;我使用 操作符去比較字符串在我的程序里面。然而&#xff0c;卻產生了一個bug&#xff0c;將這個改為了.equals()以后&#xff0c;就把bug修復了 是不是太辣雞了&#xff1f;它什么時候應該被使用或者說是不…

中小型研發團隊架構實踐三要點(轉自原攜程架構師張輝清)

如果你正好處在中小型研發團隊…… 中小型研發團隊很多&#xff0c;而社區在中小型研發團隊架構實踐方面的探討卻很少。中小型研發團隊特別是 50 至 200 人的研發團隊&#xff0c;在早期的業務探索階段&#xff0c;更多關注業務邏輯&#xff0c;快速迭代以驗證商業模式&#xf…

時間序列預測 預測時間段_應用時間序列預測:美國住宅

時間序列預測 預測時間段1.簡介 (1. Introduction) During these COVID19 months housing sector is rebounding rapidly after a downtime since the early months of the year. New residential house construction was down to about 1 million in April. As of July 1.5 mi…

zabbix之web監控

Web monitoring(web監控)是用來監控Web程序的&#xff0c;可以監控到Web程序的下載速度&#xff0c;返回碼以及響應時間&#xff0c;還支持把一組連續的Web動作作為一個整體進行監控。 1.Web監控的原理 Web監控即對HTTP服務的監控&#xff0c;模擬用戶去訪問網站&#xff0c;對…

如何使用Webpack在HTML,CSS和JavaScript之間共享變量

Earlier this week, I read an article explaining how CSS-in-JS slows down the rendering of some React apps and how static CSS is faster. But CSS-in-JS is very popular because, among other features, you can style dynamically using JavaScript variables.本周初…

Java中獲得了方法名稱的字符串,怎么樣調用該方法

問題&#xff1a; Java中獲得了方法名稱的字符串&#xff0c;怎么樣調用該方法 如果我有以下兩個變量 Object obj; String methodName "getName";在不知道obj的類的情況下&#xff0c;我怎么樣才能調用該類的名叫methodName的方法呢&#xff1f; 這個方法被調用時…

經驗主義 保守主義_為什么我們需要行動主義-始終如此。

經驗主義 保守主義It’s been almost three months since George Floyd was murdered and the mass protests. Three months since the nationwide protests, looting and riots across America.距離喬治弗洛伊德(George Floyd)被謀殺和大規模抗議活動已經快三個月了。 全國抗議…

Begin

Hello everyone, Finally,a technician from feiyang help me solve the question. Even though it is not the linux version i want.emmm...linux mint a new one i dont know about it And, lets make the life regular and delicate轉載于:https://www.cnblogs.com/lxc-run…

redis介紹以及安裝

一、redis介紹 redis是一個key-value存儲系統。和Memcached類似&#xff0c;它支持存儲的values類型相對更多&#xff0c;包括字符串、列表、哈希散列表、集合&#xff0c;有序集合。 這些數據類型都支持push/pop、add/remove及取交集并集和差集及更豐富的操作&#xff0c;而且…

java python算法_用Java,Python和C ++示例解釋的搜索算法

java python算法什么是搜索算法&#xff1f; (What is a Search Algorithm?) This kind of algorithm looks at the problem of re-arranging an array of items in ascending order. The two most classical examples of that is the binary search and the merge sort algor…

Java中怎么把文本追加到已經存在的文件

Java中怎么把文本追加到已經存在的文件 我需要重復把文本追加到現有文件中。我應該怎么辦&#xff1f; 回答一 你是想實現日志的目的嗎&#xff1f;如果是的話&#xff0c;這里有幾個庫可供選擇&#xff0c;最熱門的兩個就是Log4j 和 Logback了 Java 7 對于一次性的任務&a…

python機器學習預測_使用Python和機器學習預測未來的股市趨勢

python機器學習預測Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works withou…

線程系列3--Java線程同步通信技術

上一篇文章我們講解了線程間的互斥技術&#xff0c;使用關鍵字synchronize來實現線程間的互斥技術。根據不同的業務情況&#xff0c;我們可以選擇某一種互斥的方法來實現線程間的互斥調用。例如&#xff1a;自定義對象實現互斥&#xff08;synchronize("自定義對象")…

Python數據結構之四——set(集合)

Python版本&#xff1a;3.6.2 操作系統&#xff1a;Windows 作者&#xff1a;SmallWZQ 經過幾天的回顧和學習&#xff0c;我終于把Python 3.x中的基礎知識介紹好啦。下面將要繼續什么呢&#xff1f;讓我想想先~~~嗯&#xff0c;還是先整理一下近期有關Python基礎知識的隨筆吧…

volatile關鍵字有什么用

問題&#xff1a;volatile關鍵字有什么用 在工作的時候&#xff0c;我碰到了volatile關鍵字。但是我不是非常了解它。我發現了這個解釋 這篇文章已經解釋了問題中的關鍵字的細節了&#xff0c;你們曾經用過它嗎或者見過正確使用這個關鍵字的樣例 回答 Java中同步的實現大多是…