Python Forum

Full Version: Finding and storing all string with character A at middle position
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I need to find all strings that have particular characters at a certain index and store those words in a list. Eg; I want to find all strings that have A at index 4. I have tried to do this, but I am very much a beginner and can't get it to work. This is what I have tried:

Import random, string

random = [whizzy, mizzly,	scuzzy,	buzzed, frizzy,	fuzzed,	huzzah,	jujube,	muzzle, puzzle,	bazzed, buzzer,	buzzes,	luzzes,	fezzed,	fizzed,	fizzle,	fuzzes,	guzzle,	mezuza,	mizzen,
mizzle,pizzle]

def found_words(word):
        segment = []
        for char in word:
            if char[5] in char == 't'or 'o' or 'h':
                segment.append(char)
                return segment
            
found_pairs(random)
Thanks in advanced for any help :)
You don't need a loop, you just need to test the one character. Your conditional is messed up. The in operator should be used with a list or tuple: char[5] in ('t', 'o', 's'):.
Thank you, I got it to work :)