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

創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站設(shè)計(jì)、做網(wǎng)站與策劃設(shè)計(jì),克拉瑪依網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:克拉瑪依等地區(qū)??死斠雷鼍W(wǎng)站價(jià)格咨詢:18980820575
寫一個(gè) Python 程序,用一個(gè)實(shí)際例子找出字符串中字符的所有出現(xiàn)。
Python 程序查找字符串中的字符的所有出現(xiàn)示例 1
這個(gè) python 程序允許用戶輸入字符串和字符。這里,我們使用 For 循環(huán)來(lái)迭代字符串中的每個(gè)字符。在 Python For Loop 中,我們使用 If 語(yǔ)句來(lái)檢查 str1 字符串中的任何字符是否等于字符 ch。如果為真,則我將值打印為輸出。記住,I 是一個(gè)索引位置(從 0 開始)。
# Python Program to find Occurrence of a Character in a String
str1 = input("Please enter your own String : ")
ch = input("Please enter your own Character : ")
for i in range(len(str1)):
if(str1[i] == ch ):
print(ch, " is Found at Position " , i + 1)Python 字符串輸出中字符的所有出現(xiàn)
Please enter your own String : tutorial gateway
Please enter your own Character : t
t is Found at Position 1
t is Found at Position 3
t is Found at Position 12
Python 程序返回字符串中的字符的所有出現(xiàn)示例 2
這個(gè) Python 顯示字符串程序中字符的所有出現(xiàn)與上面相同。然而,我們只是將循環(huán)的替換為循環(huán)的。
# Python Program to find Occurrence of a Character in a String
str1 = input("Please enter your own String : ")
ch = input("Please enter your own Character : ")
i = 0
while(i < len(str1)):
if(str1[i] == ch ):
print(ch, " is Found at Position " , i + 1)
i = i + 1Python 中所有字符出現(xiàn)在一個(gè)字符串中輸出
Please enter your own String : hello world
Please enter your own Character : l
l is Found at Position 3
l is Found at Position 4
l is Found at Position 10
顯示字符串中的字符總出現(xiàn)次數(shù)的 Python 程序示例 3
這個(gè) Python 查找字符串中的字符的所有出現(xiàn)與第一個(gè)示例相同。但是,在這個(gè) python 程序中,我們使用了函數(shù)的概念來(lái)分離 Python 邏輯。
# Python Program to find Occurrence of a Character in a String
def all_Occurrence(ch, str1):
for i in range(len(str1)):
if(str1[i] == ch ):
print(ch, " is Found at Position " , i + 1)
string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")
all_Occurrence(char, string) 分享題目:Python程序:查找字符串中的字符的所有出現(xiàn)
轉(zhuǎn)載源于:http://fisionsoft.com.cn/article/dhijccg.html


咨詢
建站咨詢
