'How to get a part of a string from the start to a specific point

I'm working in python and I'm trying to find a way to get part of a substring from the start to a point.

this function takes user input so I cannot use anything that relies on a set index.

some examples for input would be [email protected] where I'm trying to get 'john'

or [email protected] for the output 'wheres'

here's what I have now

getnamelast= s[s.index('.')+1:s.index('@')]
print(getnamelast)

which I know only return everything after '.'



Solution 1:[1]

If you want to get the first name (from start of the string till .), you can use str.split. For example:

s = "[email protected]"

name = s.split(".")[0]
print(name)

Prints:

john

Solution 2:[2]

Try regex

firstname = re.findall(r'^[\w]*', s)
print(firstname)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrej Kesely
Solution 2 John R