Format () in Python Regex

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

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

You can put the literal { , } with {{ and }} :

 >>> "{0}\w{{2}}b{1}\w{{2}}quarter".format('b', 'a') 'b\\w{2}ba\\w{2}quarter' 
+6
source

Source: https://habr.com/ru/post/1500469/


All Articles