whatsupmyname
whatsupmyname

Reputation: 31

Finding first occurrence of character and then printing WORD (dont want the index)

I want to find the first occurrence of a specific letter and then print out the word where the character first appears. The letter can be upper or lower case.

Upvotes: 0

Views: 51

Answers (1)

Marcin-99
Marcin-99

Reputation: 51

You can use split() method:

def fun(string, char):
    for word in string.split():
        if char in word:
            return word
    return None


print(fun('abc def ghjk ldsa l', 'e'))

Above code will result in:

def

Upvotes: 1

Related Questions