av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

Python程序:計算字符串中的字符出現(xiàn)次數(shù)

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

創(chuàng)新互聯(lián)公司專注于宜良企業(yè)網(wǎng)站建設,自適應網(wǎng)站建設,商城網(wǎng)站開發(fā)。宜良網(wǎng)站建設公司,為宜良等地區(qū)提供建站服務。全流程按需定制開發(fā),專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務

寫一個 Python 程序來計算字符串中的字符的出現(xiàn)次數(shù),并給出一個實例。這個 python 程序允許您輸入字符串和字符。

# Python Program to Count Occurrence of a Character in a String

string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")

count = 0
for i in range(len(string)):
    if(string[i] == char):
        count = count + 1

print("The total Number of Times ", char, " has Occurred = " , count)

這里,我們使用 For Loop 來迭代一個字符串中的每個字符。在 Python For Loop 中,我們使用 If 語句來檢查字符串中的任何字符是否等于給定的字符。如果為真,則計數(shù)=計數(shù)+ 1。

字符串=教程網(wǎng)關 ch = t 計數(shù)= 0

對于循環(huán)第一次迭代:對于范圍(11)中的 I,如果(字符串[i] == char) 如果(t = = t)–條件為真。 計數(shù)= 0 + 1 = > 1

第二次迭代:如果(u = = l)–條件為假,范圍(11) 中的 1。

第三次迭代:對于范圍(11)中的 2,如果(字符串[2] == char) = >如果(t = = t)–條件為真。 計數(shù)= 1 + 1 = > 2

對剩余的程序迭代進行同樣的操作

計算字符出現(xiàn)次數(shù)的 Python 程序示例 2

這個 Python 計算字符串程序中一個字符的總出現(xiàn)次數(shù)與上面相同。然而,我們只是將 For 循環(huán)替換為 While 循環(huán)。

# Python Program to Count Occurrence of a Character in a String

string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")
i = 0
count = 0

while(i < len(string)):
    if(string[i] == char):
        count = count + 1
    i = i + 1

print("The total Number of Times ", char, " has Occurred = " , count)

字符串輸出中 python 字符的出現(xiàn)

Please enter your own String : python programs
Please enter your own Character : p
The total Number of Times  p  has Occurred =  2
>>> 
Please enter your own String : hello
Please enter your own Character : l
The total Number of Times  l  has Occurred =  2

計算字符總出現(xiàn)次數(shù)的 Python 程序示例 3

給定字符程序的 Python 總出現(xiàn)次數(shù)與第一個示例相同。但是,這一次,我們使用了函數(shù)的概念來分離邏輯。

# Python Program to Count Occurrence of a Character in a String

def count_Occurrence(ch, str1):
    count = 0
    for i in range(len(string)):
        if(string[i] == char):
            count = count + 1
    return count

string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")

cnt = count_Occurrence(char, string)
print("The total Number of Times ", char, " has Occurred = " , cnt)

字符串輸出中 python 字符的出現(xiàn)

Please enter your own String : Python tutorial
Please enter your own Character : t
The total Number of Times  t  has Occurred =  3
>>> 
Please enter your own String : hi
Please enter your own Character : g
The total Number of Times  g  has Occurred =  0

網(wǎng)頁標題:Python程序:計算字符串中的字符出現(xiàn)次數(shù)
網(wǎng)站鏈接:http://uogjgqi.cn/article/dhsjipc.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流