Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialahmedagour
Courses Plus Student 216 PointsI 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
3 Answers
yk7
Full Stack JavaScript Techdegree Student 22,891 Points matchobj = re.findall(r'\d', searchstring)
ahmedagour
Courses Plus Student 216 Pointsbut doesn't this r' to tell python it is a string ?
yk7
Full Stack JavaScript Techdegree Student 22,891 PointsI 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!