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 trial

Python Regular Expressions in Python Introduction to Regular Expressions Escapes

escape.py

I've tried many variations some of which I did not create my own string variable.

Am I supposed to define a function here with -- def ? I'm not sure what to put inside the parentheses when treehouse asks me to write or define a function with a string a string argument. In these instances should I: put '' or "" inside the parentheses, put 'someString' inside the parentheses, create a variable that is string (e.g., x in my attached code), or whether not to put anything inside the parentheses of the def function_name():

Thanks for any help you can lend!

escapes.py
import re
x = "some_string567"
def first_number(x):
    first_num = re.match(r'\d', x)
    return first_num

1 Answer

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,073 Points

When they ask you to define a function that receives a string argument you just have to think that the argument that you are going to put inside the parenthesis is a string, no need to put extra prefix or postfix to denote that. For this code challenge you have to use the ".search()" function.

Remember the ".search()" search a pattern anywhere in the string, the ".match()" function search a pattern at the beginning of the text.

import re

def first_number(str_arg):
    return re.search(r'\d', str_arg)

I hope that helps a little bit.