我正在尝试创建一个程序,该程序将输入一个句子和一个单词,并检查该句子中的单词并返回单词开头的索引 即
sentence = "random set of words"
word = "of"
result = 9
这是我尝试过的
sentence = (input("Enter a sentence: "))
word = (input("Enter a Word: "))
result = sentence.split()
for x in sentence:
if x == (word):
boom = enumerate(word)
print(boom)
最佳答案
只需使用索引()
a =“aa bb cc”
b =“bb”
打印a.index(b)
如果OP希望它只计算字母:
a =“aa bb cc”
b =“bb”
index_t = a.index(b)
real_index = index_t - a[:index_t].count(' ')
关于Python:获取列表的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46166612/