1

The user enters, for example, "Hello". How to make the "print" output the indexes of all the letters l

if "l" in user_sms:
    print()
Amsuduf
  • 7
  • 2
  • Does this answer your question? [How to find all occurrences of a substring?](https://stackoverflow.com/questions/4664850/how-to-find-all-occurrences-of-a-substring) – luk2302 Nov 14 '22 at 12:13
  • Since you're only looking for single-letters, you could also [enumerate](https://docs.python.org/3/library/functions.html#enumerate) through the string, printing the index if the item is "l" – hopperelec Nov 14 '22 at 12:15

2 Answers2

1

Check if this solves your problem

for i in range(len(user_sms)):
    if user_sms[i]=='l':
       print(i)
Khaled DELLAL
  • 871
  • 4
  • 16
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Yevhen Kuzmovych Nov 14 '22 at 12:28
  • Thanks @YevhenKuzmovych I will edit my answer and add an explanation – Khaled DELLAL Nov 14 '22 at 15:20
1

Loop over the characters in the string using the enumerate function, the enumerate function returns an iterable (index, item).
Example:

user_sms = "Hello"
enumerate(user_sms) -> (0, "H"), (1, "e"), ...

We can use this to loop the string, and check if the letter is l.

for i, letter in enumerate(user_sms):
  if letter == 'l':
    print(i) # prints the index

However, if you also want to detect uppercase and lowercase L's, you need to use the .lower() function, this function returns the lowercased string, which makes uppercase L to be lowercase.

for i, letter in enumerate(user_sms.lower()):
  if letter == 'l':
    print(i)
1mmunity
  • 54
  • 4