我想寫一個程序來改變字符,如果ch被發現在名爲st的字符串中,我會用'!'替換它。如何替換字符串中的子字符串?
我寫了一個程序,但由於某些原因,如果我輸入驗證碼不能取代例如一個字母:「!」 ST =一個 CH =一個
我不得到的輸出相反,我得到'一個',但我不希望我想它是'!'
我的代碼是
st = raw_input("String: ")
ch = raw_input("character: ")
def replace_char(st,ch):
if st.find(ch):
new = st.replace(ch,'!')
print new
return new
elif len(st)==len(ch):
if ch==st:
print"!"
else:
print st
else:
print st
return st
replace_char(st,ch)
請幫助我不明白我在做什麼錯誤或從我的代碼
使用合理的主題 - > Downvote。 –
str.find()不返回True或False,它返回找到的字符串的索引。 –
對不起,這是舊的,我改變它,但它仍然不會工作.... –