新聞中心
創(chuàng)新互聯(lián)Python教程:

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),萊州企業(yè)網(wǎng)站建設(shè),萊州品牌網(wǎng)站建設(shè),網(wǎng)站定制,萊州網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,萊州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
寫一個 Python 程序,使用 For 循環(huán)和 While 循環(huán)計算字符串中的總字符數(shù),并給出一個實例。
計算字符串中的字符總數(shù)的 Python 程序示例 1
這個 python 程序允許用戶輸入一個字符串。接下來,它使用 For 循環(huán)計算該字符串中的字符總數(shù)。
這里,我們使用 Python For 循環(huán)來迭代字符串中的每個字符。在 For 循環(huán)中,我們遞增每個字符的總值。
# Python Program to Count Total Characters in a String
str1 = input("Please Enter your Own String : ")
total = 0
for i in str1:
total = total + 1
print("Total Number of Characters in this String = ", total)Python 程序計算字符串中的字符數(shù)示例 2
這個字符串字符程序和上面的例子一樣。然而,在這個 Python 代碼中,我們使用了帶有范圍的 For Loop 。
# Python Program to Count Total Characters in a String
str1 = input("Please Enter your Own String : ")
total = 0
for i in range(len(str1)):
total = total + 1
print("Total Number of Characters in this String = ", total)Python 計數(shù)字符串輸出
Please Enter your Own String : Tutorial Gateway
Total Number of Characters in this String = 16
>>>
Please Enter your Own String : Python
Total Number of Characters in this String = 6計算字符串中的字符數(shù)的 Python 程序示例 3
這個 python 程序?qū)ψ址M行計數(shù)同上。然而,我們只是將 For 循環(huán)替換為 While 循環(huán)。
# Python Program to Count Total Characters in a String
str1 = input("Please Enter your Own String : ")
total = 0
i = 0
while(i < len(str1)):
total = total + 1
i = i + 1
print("Total Number of Characters in this String = ", total)Please Enter your Own String : Python Programming
Total Number of Characters in this String = 18 網(wǎng)頁標(biāo)題:Python程序:計算字符串中的字符總數(shù)
網(wǎng)頁網(wǎng)址:http://m.fisionsoft.com.cn/article/ccdshis.html


咨詢
建站咨詢
