Python Forum
Finding and storing all string with character A at middle position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding and storing all string with character A at middle position
#1
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 :)

Reply
#2
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'):.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you, I got it to work :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  position of shortest string Ali_ 2 1,467 Mar-17-2022, 03:48 PM
Last Post: DeaD_EyE
  Finding how many times substring is in a string using re module ranbarr 4 2,943 May-21-2021, 06:14 PM
Last Post: nilamo
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,573 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  Replacing a few characters of a specified character but not all of them from a string fatherted99 7 3,195 Aug-13-2020, 09:08 AM
Last Post: fatherted99
  Tab character in a string prints 8 spaces hecresper 6 20,524 Aug-27-2019, 02:38 PM
Last Post: snippsat
  Unexpected character after line continuation character joshyb123 5 10,620 Sep-05-2018, 08:08 AM
Last Post: perfringo
  unexpected character after line continuation character error newbie 10 14,613 Aug-09-2018, 06:07 PM
Last Post: nilamo
  Display middle character/s jmcatena91 2 23,154 Feb-01-2018, 11:45 PM
Last Post: jmcatena91
  Finding repetition in string student8 4 5,046 Oct-15-2017, 07:26 PM
Last Post: student8

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020