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()
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()
Check if this solves your problem
for i in range(len(user_sms)):
if user_sms[i]=='l':
print(i)
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)