4

In the example below, I want {0} and {1} to be replaced by the arguments passed to format, and {2} should have the regex functionality with \w{2} of, "find two letter characters". How do I do this?

q = re.search("{0}\w{2}b{1}\w{2}quarter".format('b', 'a'), search_me).group()

1 Answer 1

6

You can put literal {, } using {{ and }}:

>>> "{0}\w{{2}}b{1}\w{{2}}quarter".format('b', 'a')
'b\\w{2}ba\\w{2}quarter'
Sign up to request clarification or add additional context in comments.

1 Comment

+1, Beat me to it (stupid minimum character amount requirement in answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.