Last active
December 15, 2021 03:16
-
-
Save kkew3/f38d8d1c023784ccfdb18dec9c4392e1 to your computer and use it in GitHub Desktop.
Count number of placeholders in a `str.format` template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
# Reference: https://stackoverflow.com/a/46161774/7881370 | |
# Access date: 2019-03-23 | |
def count_n_placeholders(template: str) -> int: | |
return sum(1 for _ in filter(lambda x: x[1] is not None, | |
string.Formatter().parse(template))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment