Welcome to the Treehouse Community
Looking to learn something new?
I have wrote a function that returns the first digit in a string I am not sure what is the problem in my code
Not sure what the questions is asking of me
import re def first_number(searchstring): matchobj = re.match(r'\d', searchstring) return matchobj
matchobj = re.findall(r'\d', searchstring)
but doesn't this r' to tell python it is a string ?
I tested your code in pycharm , and it's good, the string must start with digit and it returns a re.Match Class.
import re def first_number(searchstring) : ''' extracting digit from string ''' a = re.match('\d', searchstring) print(type(a)) print(a) first_number("4Lives")
and it returns :
<class 're.Match'> <re.Match object; span=(0, 1), match='4'>
if you want to grab a digit from a string regardless where it positioned in the string, "live4ever" you need to use findall() instead of match().
you are right , r is for raw. and has no effect here. sorry about before!