+ 1

What python code prints out the position of a letter in a word?

it's for guess the word game

5th Feb 2017, 8:02 PM
Pride Mawire
Pride Mawire - avatar
2 ответов
+ 6
word = "metaphore" print(word.find("a")) print(word.index("a")) # both methods print out 3, but index should be used only if you are sure the argument is to be found. If not found, word.find returns -1, while word.index crashes the program.
5th Feb 2017, 9:55 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
variable_containing_word.find(variable_containing_letter) if the word is 'game' and the letter is 'a', then the above code will return 1 (since the index starts with 0). And if it can't find the letter, it would return -1.
5th Feb 2017, 9:31 PM
Alex