2016-01-22 134 views
0

有沒有更簡單的方法來檢查字符串中的字符?謝謝:dPython 3.x短版本的代碼

check = input("Put in the letter: ") 
word = "word" 

if(check == word[0]): 
    print(check) 
if(check == word[1]): 
    print(check) 
if(check == word[2]): 
    print(check) 
if(check == word[3]): 
    print(check) 

回答

3

喜歡的東西

for l in word: 
     if l == check: 
      print (check) 

也許?

+0

快速注:@leistungsabfall的代碼將打印的字符一次eventhough而我的代碼像你原來的代碼打印n次,如果發生信多次,可能會發生多次,在這個詞裏。 –

5

如何:

if check in word: 
    print(check) 
+2

這不完全相同,例如如果'word ='HELLO''和'check ='L'' –