a is actually slightly smaller than 1.555 due to imprecise representation of floating point values.

You can see this, for example, as follows:

f"{a:.40f}"
'1.5549999999999999378275106209912337362766'

Or,

f"{a:.64f}"
'1.5549999999999999378275106209912337362766265869140625000000000000'
Answer from user650654 on Stack Overflow
🌐
Python
bugs.python.org › issue35432
Issue 35432: str.format and string.Formatter bug with French (and other) locale - Python tracker
December 6, 2018 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/79613
🌐
Python
bugs.python.org › issue12014
Issue 12014: str.format parses replacement field incorrectly - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/56223
Discussions

python - str.format() option is not working - Stack Overflow
It does work. You were just not assigning the format to a variable, and then just printing the original string. More on stackoverflow.com
🌐 stackoverflow.com
Python string format error - Stack Overflow
This is pretty basic: my_name="nishant" my_age=24 print "My name is %s"%my_name print "My age is %d & my name is %s, what are you ?" %(my_age, my_name) It works fine and prints the intended re... More on stackoverflow.com
🌐 stackoverflow.com
May 3, 2017
Is format string still an issue in Python? - Information Security Stack Exchange
I am not familiar with Python. I found lots of places talking about the format string issue in python. To understand its impact, I created a very simple test code: # assume this CONFIG holds sensit... More on security.stackexchange.com
🌐 security.stackexchange.com
Very basic string formatting not working (Python) - Stack Overflow
I am just learning python and attempting a very simple string formatting line, but it's not working properly. As you can see from the output below, it is inserting the number in the last of the 3, ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Wallarm
wallarm.com › what › format-string-vulnerability
✋Format String Vulnerability - Types, Examples, Prevention
June 26, 2025 - Format string attack is often found in C language programs, it refers to a bug found in the printf() function. Format string vulnerabilities in Python.
🌐
Stack Exchange
security.stackexchange.com › questions › 269949 › is-format-string-still-an-issue-in-python
Is format string still an issue in Python? - Information Security Stack Exchange
format string vulnerability is about using a format string (in your case Job ID: {}) which includes untrusted user input. Your example does not do this, i.e. your format string is a static string hard coded in the program.
Top answer
1 of 3
4

The problem here is that the format method is only being called for the final string in the 3 strings being added together. You should either apply the format operation AFTER you have concatenated the strings, or use a single string format that accepts line breaks (so you don't have to concatenate strings in the first place).

To apply after concatenation, you can just use an extra set of parentheses around the string concatenation, e.g.

 print(("Here are three other numbers." + \
" First number is {0:d}, second number is {1:d}, \n" + \
"third number is {2:d}") .format(7,8,9))

Or you can use triple quotes to accept line breaks, e.g.

print("""Here are three other numbers.\
First number is {0:d}, second number is {1:d},
third number is {2:d}""" .format(7,8,9))

where \ allows a line break in the code that wont be printed.

2 of 3
2

You have a precedence issue. What you are actually doing is concatinating three strings - "Here are three other numbers.", " First number is {0:d}, second number is {1:d}, \n" and the result of "third number is {2:d}" .format(7,8,9). The format call is only applied to the third string, and therefore only replaces the {2:d}.

One way to solve this could be to surround the string concatination you intended to have with parentheses (()) so it's evaluated first:

print(("Here are three other numbers." + \
    " First number is {0:d}, second number is {1:d}, \n" + \
    "third number is {2:d}") .format(7,8,9))

But a much cleaner approach would be to drop the string concatination altogether and just use a multiline string (note the lack of the + operator):

print("Here are three other numbers." \
    " First number is {0:d}, second number is {1:d}, \n" \
    "third number is {2:d}" .format(7,8,9))
Find elsewhere
🌐
Armin Ronacher
lucumr.pocoo.org › 2016 › 12 › 29 › careful-with-str-format
Be Careful with Python’s New-Style String Format | Armin Ronacher's Thoughts and Writings
December 29, 2016 - For as long as only C interpreter objects are passed to the format string you are somewhat safe because the worst you can discover is some internal reprs like the fact that something is an integer class above. However tricky it becomes once Python objects are passed in.
Top answer
1 of 2
6

You are missing the formatter type:

%(other_id)s

Note the s after the parenthesis; you want to interpolate the values as strings. Here is a working version instead:

jQuery = ("$('#%(other_id)s').click(function() { "
                  "    if ($(this).is(':checked')) { "
                  "        $('#%(text_id)s').show()     "
                  "    } "
                  "    else {"
                  "       $('#%(text_id)s').hide()"
                  "    }"
                  " });")

Dollar symbols have no meaning in %-style string formats and I've added the # id selectors for you. :-)

Personally, I would use """ triple-quotes instead:

jQuery = """\
$('#%{other_id}s').click(function() {
    if ($(this).is(':checked')) {
        $('#%(text_id)s').show()
    }
    else {
        $('#%(text_id)s').hide()
    }
});
"""

Better still, put this into a Jinja template anyway (since you are using Flask) and render that instead:

jquery = render_template('toggle_field.js', other_id=choice_id, text_id=text_choice_id)

where toggle_field.js is the Jinja template version of the jQuery snippet:

$('#{{ other_id }}').click(function() {
    if ($(this).is(':checked')) {
        $('#{{ text_id }}').show()
    }
    else {
        $('#{{ text_id }}').hide()
    }
});
2 of 2
0

Instead of code generation, consider an approach that is data-driven. Define the following two functions statically, preferably in some js file you include across all your html files:

function toggle_if_checked(checkbox, toggleable) {
    var cbox = $(checkbox), tgl = $(toggleable);
    tgl.toggle(cbox.is(':checked'));
}

function register_check_show_events(elist) {
    var i, cboxselector, textselector;
    function handler(e) {
        toggle_if_checked(e.target, e.data);
    }
    for (i = 0; i < elist.length; i++) {
        cboxselector = '#'+elist[0];
        textselector = '#'+elist[1];
        $(cboxselector).on('click', null, textselector, handler);
    }
}

Then to register your event handlers, collect a Python list of ids and make it available to javascript via JSON.

import json

ids = [('cboxid1','textboxid1'),('cboxid2','textboxid2')]
json_ids = json.dumps(ids)
script = 'register_check_show_events({});'.format(json_ids)

In general, your code will be cleaner and easier to maintain if you only pass data between your Python and JS layers via JSON rather than generate javascript code dynamically.

🌐
GitHub
github.com › python › cpython › issues › 73754
undefined parsing behavior with the old style string formatting · Issue #73754 · python/cpython
February 15, 2017 - assignee = None closed_at = <Date 2017-03-08.03:51:57.349> created_at = <Date 2017-02-15.14:53:02.770> labels = ['interpreter-core', 'type-bug', '3.7'] title = 'undefined parsing behavior with the old style string formatting' updated_at = <Date 2017-03-24.22:43:13.732> user = 'https://bugs.python....
Author   python
🌐
Reddit
reddit.com › r/learnpython › string format syntax error
r/learnpython on Reddit: string format syntax error
December 4, 2023 -

Hello, I'm trying to format else statement here. The goal is to suppress automatic new line due to print function and get a person with a single favorite language in a single sentence. When I run the code, IDLE highlights the opening quotation in the else block print statement returning "Invalid Syntax. Perhaps you forgot a comma?".

I have tried the same formatting (without else) in a practice code and it's working. I don't know what I'm doing wrong here? Also, is there any other formatting trick to get one line sentence for single favorite language?

favorite_languages = {
'jen': ['python', 'rust'], 
'sarah': ['c'], 
'edward': ['rust', 'go'], 
'phil': ['python', 'haskell'], }

for name, languages in favorite_languages.items():
    if len(languages)>1:
        print(f"\n{name.title()}'s favorite languages are:") 
    else: 
        print("\n{}'s favorite language is: "format(name.title()),end='') 
    for language in languages: 
        print(f"\t{language.title()}")

🌐
Python
bugs.python.org › issue28385
Issue 28385: Bytes objects should reject all formatting codes with an error message - Python tracker
October 7, 2016 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/72571
🌐
SQLPad
sqlpad.io › tutorial › solving-pythons-not-enough-arguments-for-format-string-error
Solving Python's 'Not Enough Arguments for Format String' Error
April 29, 2024 - In the realm of Python programming, the 'not enough arguments for format string' error can be a common stumbling block, particularly for those new to string formatting. However, with proactive measures and adherence to best practices, developers ...
🌐
Python.org
discuss.python.org › ideas
Better error message for string formatting - Ideas - Discussions on Python.org
February 10, 2024 - what about throwing an exception as argument is provided without any format specifier for scenarios like below` str = "jkg" ch = "ft" new_str = str % ch ➜ cpython git:(main) ✗ ./python.exe l.py Traceback (most recent call last): File "/Users/agent-hellboy/cpython/l.py", line 5, in s = l % (p,k) TypeError: argument is provided without any format specifier
🌐
Reddit
reddit.com › r/python › despite f-string formatting being the newest way of writing formatted strings, why do lots of people still use the old style % formatting in python?
r/Python on Reddit: Despite f-string formatting being the newest way of writing formatted strings, why do lots of people still use the old style % formatting in Python?
August 14, 2021 -

I've watched recent Python videos, and even after reading some of the documentation, it is recommended to use f-string formatting rather than the older string formatting methods. Despite this, I still see lots of people using the older % formatting which is less readable, and in general takes more time to write with.

🌐
GitHub
github.com › python › cpython › issues › 119488
String format specifiers not accepted by object.__format__ · Issue #119488 · python/cpython
May 24, 2024 - Bug report Bug description: Given an instance of a class with __str__ defined: class Foo: def __str__(self): return 'foo' foo = Foo() Formatting it will implicitly convert the object to a string: print(f'{foo}') # outputs "foo" But addin...
Author   python
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
The feature described here was introduced in Python 2.4; a simple templating method based upon regular expressions. It predates str.format(), formatted string literals, and template string literals.