2016-01-22 140 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'' –