Omar
Omar

Reputation: 11

print after a certain word in python

In Python, I would like to read the input and then only print after a certain point. I would like for it to work like this

    humaninput = raw_input("Please enter:")
    breakdown = humaninput.split()
    say = "say"
    if say in breakdown:
        print (all words after say)

I have everything except for the last part

Upvotes: 1

Views: 4785

Answers (3)

Charles Clayton
Charles Clayton

Reputation: 17946

Here's a neat alternative that doesn't use split.

string = "string blah say foo bar"
say = "say"
after = string[string.index(say) + len(say):] # +1 if you're worried about spaces
print(after)

>> foo bar

And if there are multiple instances of "say", it will take the first.

Upvotes: 1

TheSoundDefense
TheSoundDefense

Reputation: 6935

This is pretty easy to do with split() if you're just using a string.

if say in humaninput:
  saysplit = humaninput.split(say,1)
  print saysplit[1]

It works on entire strings, not just single characters or nothing at all (where it defaults to space). If you've got a list, the other answer is correct.

Upvotes: 0

Stack of Pancakes
Stack of Pancakes

Reputation: 1921

Since you converted all the entries in to a list you can find the first instance of "say", then make a new list with everything after it.

humaninput = "This is me typing a whole bunch of say things with words after it"
breakdown = humaninput.split()
say = "say"
if say in breakdown:
    split = breakdown.index(say)
    after = breakdown[split+1:]
    print(after)

Upvotes: 0

Related Questions