What am I doing wrong here?
import re x = 'The sky is red' r = re.compile ('red') y = r.sub(x, 'blue') print x # Prints 'The sky is red' print y # Prints 'blue'
How do i get it to print ‘The sky is blue’?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem with your code is that there are two sub functions in the re module. One is the general one and there’s one tied to regular expression objects. Your code is not following either one:
The two methods are:
re.sub(pattern, repl, string[, count])(docs here)Used like so:
And for when you compile it before hand, as you tried, you can use:
RegexObject.sub(repl, string[, count=0])(docs here)Used like so: