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 '.'
CodePudding user response:
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
CodePudding user response:
Try regex
firstname = re.findall(r'^[\w]*', s)
print(firstname)