I have a string and i need to loop through all of its letter including (if there’s any) white spaces, numbers, symbols, etc…
I have to make sure that the string only contains letters, but my loop only goes through the first letter and then it produces an output straight away thus missing any symbols, white spaces in between letters.
i tried using for loop and even while loop but it’s not giving me the correct output
for char in text: if char in letter_list: print('the word is in the list') elif char not in letter_list: print('the word is not in the list') break
the string word is ca nada. there is a white space between ca and nada- if that’s the case the output will print:
the letter is not in the list
Difficult to know what you want without knowing what is in the letter_list. Is the letter c not in that list? Please provide the contents of this "letter_list" for clarification.
This will loop through all the letters and print the correct line if they are in the list or not. I just removed the break
import string text = "ca nada" letter_list = string.ascii_letters for char in text: if char in letter_list: print('the letter is in the list') elif char not in letter_list: print('the letter is not in the list')