Skip to content

Instantly share code, notes, and snippets.

@kkew3
Last active December 15, 2021 03:16
Show Gist options
  • Save kkew3/f38d8d1c023784ccfdb18dec9c4392e1 to your computer and use it in GitHub Desktop.
Save kkew3/f38d8d1c023784ccfdb18dec9c4392e1 to your computer and use it in GitHub Desktop.
Count number of placeholders in a `str.format` template
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