Determine if the string is formatable

import string

formatter = string.Formatter()


def is_formattable(s: str):
    for e in formatter.parse(s):
        if isinstance(e[1], str):
            return True
    return False


print(is_formattable("Hello"))  # False
print(is_formattable("{{}}"))  # False (Double escape)
print(is_formattable("{}"))  # True
print(is_formattable("{}{}"))  # True
print(is_formattable("{a}{b}{c}"))  # True
print(is_formattable("{a:int}"))  # True


Recommended Posts

Determine if the string is formatable
Determine if the library is installed.
Check if the string is a number in python
Determine if an attribute is defined in the object
Determine if a string is a time with a python regular expression
Determine if AWS Chalice is chalice local
[Golang] Check if a specific character string is included in the character string
[Python] Determine if any coordinate point is inside or outside the polygon
[Nohup] Execution even if the terminal is turned off
Check if it is Unix in the scripting language
Determine if the member should be set to the default string with just the or operator
Check if the LAN cable is disconnected on Linux
Check if it is Unix in the scripting language
I want to initialize if the value is empty (python)
[Pandas] If the first row data is in the header in DataFrame
Find out how many each character is in the string.
What is the activation function?
What is the Linux kernel?
Is this string a decimal?
[Python 2/3] Parse the format string
What is the interface for ...
What is the Callback function?
The image is a slug
Python will fail if there is a space after the backslash
[Blender] Script to check if the selected one is a mesh
python Note: Determine if command line arguments are in the list
What to do if the inode is exhausted on EC2 Linux
Determine if all list elements are present in the dict key
Determine if standard output is piped when running a Python script
Have python check if the string can be converted / converted to int
If the accuracy of the PCR test is poor, why not repeat the test?