Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Regular Expressions in Python Introduction to Regular Expressions Escapes

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

3 Answers

but doesn't this r' to tell python it is a string ?

Full Stack JavaScript Techdegree Student 22,891 Points

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)


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!