0

Is there a way shorter way to check for characters in a string? Thanks :D

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)
Ziggster
  • 21
  • 2

2 Answers2

5

How about this:

if check in word:
    print(check)
Leistungsabfall
  • 6,368
  • 7
  • 33
  • 41
4

Something like

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

Maybe ?

Work of Artiz
  • 1,085
  • 7
  • 16
  • Quick note: the code of @leistungsabfall will print the character once eventhough it might occur multiple times, whereas my code like your original code prints n times if the letter occurs multiple times in the word. – Work of Artiz Jan 22 '16 at 23:45