如何让用户(输入)从句子/字符串中选择一个单词,然后使用python 3.4.3打印所选单词所在的位置
答案 0 :(得分:1)
>>> foo = 'This is a sentence'
>>> foo
'This is a sentence'
>>> choice = input('Choose a word from the sentence ' )
Choose a word from the sentence is
>>> foo = foo.split()
>>> foo
['This', 'is', 'a', 'sentence']
>>> if choice in foo:
... print(foo.index(choice))
...
1
>>>
从列表切换回字符串
>>> foo = ' '.join(foo)
>>> foo
'This is a sentence'
>>>