count instead of max | SoloLearn: Learn to code for FREE!


count instead of max

Hello, shouldn't the right definition in the course (Regular Expressions -> Introduction -> Search&Replace) be def re.sub(pattern, repl, string, count) rather than the mentioned def re.sub(pattern, repl, string, max)? If not, may I kindly ask you to explain why? Thanks for checking!

2/19/2017 3:20:25 PM

Margarete Müller

4 Answers

New Answer


The easiest way to work with it is to write re.sub(..., 1) if you want to replace the first pattern. But you are right, it works with count=1 instead of max=1


The mentioned "max=0" is correct. re.sub(...) doesn't count how often you replace a pattern. The max value can be used to set the maximal number of patterns to be replaced. If max=0, all patterns are replaced


Thanks for your reply. When using max the code shows an error message. Replacing max with count works. That's what confuses me. Have you checked the code in the course example?


Thanks. I think I'll use the shorter version :-)