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 The Basics

Import re. Create an re.match() for the word "Four" in the data variable. Assign this to a new variable named first.

error says

first doesnt seem to be a regex blabla. Cant really figure wher i am missing it

basics.py
import re

file_object = open("basics.txt", encoding="utf-8")
data = file_object.read()
file_object.close()

first = r'Four'
print(re.match(first, data))

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, Simbarashe Chibgwe you should assign the re.match result to variable first, then print it.

first = re.match(r'Four', data)
print(first)

Thank you so much, it worked. But for the sake of learning, can u point out what is wrong with my first code?? I guess that way should have worked as well, but it did not.

Any suggestions

William Li
William Li
Courses Plus Student 26,868 Points

Sure, but first I must say that I got a little carried away by your code and didn't read the challenge description of Part 3 very carefully.

Import re. Create an re.match() for the word "Four" in the data variable. Assign this to a new variable named first.

There's really no need for the print statement at the second line.

first = re.match(r'Four', data)

Is all the Challenge is asking for.


So now back to your question. Obviously the grader for this part is checking whether your variable first is holding the correct value; therefore,

# your code
first = r'Four'

# my code
first = re.match(r'Four', data)

You should be able to see that these two first variable don't hold the same value, right? Hope that helps.

Look at myself, hahaha thanks again. You just uncovered the whole secret here.

I appreciate, and now i can use the trick with confidence

file_object = open("basics.txt") data = file_object.read() file_object.close() import re first = re.match(r'Four', data)