0

How to get index of a letter?

I tried to find the index of a word using .index() but im not sure if thats how u find the index...? Can someone help please? Edit: programming language is python

18th Dec 2019, 11:06 PM
Vlad
Vlad - avatar
2 ответов
+ 7
str has index AND find, list has only index. In a string, you can find single letters, but also longer substrings (words) with both methods. The difference is that find, if it *doesn't* find the substring, returns -1, and index will throw an error. The problem, when you try to find, let's say, 'and' in a long string, you will also find the ands in words like hand, panda, sandy etc. When I want to work with words in texts, I will first split the text into words, using the method 'split'. 'Hello World'.split() leads to ['Hello', 'World']. Now you can easily find words using index, but single letters doesn't work anymore.
18th Dec 2019, 11:47 PM
HonFu
HonFu - avatar
+ 4
in python : for strings use string.find("x") for lists use list.index("x") they return the position of x.
18th Dec 2019, 11:13 PM
Bahhaⵣ
Bahhaⵣ - avatar