About 173,000 results
Open links in new tab
  1. python - String formatting: % vs. .format vs. f-string literal - Stack ...

    It allows logging with format without the performance hit -- does it by overriding __str__ precisely as logging was designed for -- shortens the function call to a single letter (N) which feels very similar to some of the standard ways to define strings -- …

  2. python - How to print a percentage value? - Stack Overflow

    Mar 15, 2011 · Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. The .0 part of the format spec .0% indicates that you want zero digits of precision after the decimal point, because with f"{1/3:%}" you would get the string '33.333333%'. It works with integers, floats, and decimals. See PEP 3101.

  3. String formatting in Python - Stack Overflow

    Python String format ... ways of formatting a string in python, like: Using the format() function, ...

  4. Format numbers to strings in Python - Stack Overflow

    Python 2.6+ It is possible to use the format() function, so in your case you can use: return '{:02d}:{:02d}:{:.2f} {}'.format(hours, minutes, seconds, ampm) There are multiple ways of using this function, so for further information you can check the documentation. Python 3.6+ f-strings is a new feature that has been added to the language in ...

  5. python - How can I format a decimal to always show 2 decimal …

    the Python String Format Cookbook: shows examples of the new-style .format() string formatting; pyformat.info: compares the old-style % string formatting with the new-style .format() string formatting; Python 3.6 introduced literal string interpolation (also known as f-strings) so now you can write the above even more succinct as: >>> f'{pi:.2f ...

  6. What does format () in Python actually do? - Stack Overflow

    Jul 29, 2020 · Also, there are two type of format in python 3x: the format expression and the format function. the format expression is actually this '%' and many more on 'format'. Maybe you should check on the doc 'format' by typing "help(''.format)"

  7. How to use str.format () with a dictionary in Python?

    Aug 19, 2022 · There is ''.format_map() function since Python 3.2: test = "I have one {fruit} on the {place}.".format_map(dic) The advantage is that it accepts any mapping e.g., a class with __getitem__ method that generates values dynamically or collections.defaultdict that allows you to use non-existent keys. It can be emulated on older versions:

  8. python - Display a decimal in scientific notation - Stack Overflow

    Aug 2, 2011 · As an aside, despite the format % values syntax still being used even within the Python 3 standard library, I believe it's technically deprecated in Python 3, or at least not the recommended formatting method, and the current recommended syntax, starting with Python 2.6, would be '{0:.2E}'.format(Decimal('40800000000.00000000000000')) (or '{:.2E}' in Python 2.7+).

  9. Python Decimals format - Stack Overflow

    Mar 6, 2010 · Here's a function that will do the trick: ... the short format style from Python v3.6 on: f'{1.234:.1f ...

  10. Rounding decimals with new Python format function

    Nov 9, 2011 · Here's a typical, useful example...: >>> n = 4 >>> p = math.pi >>> '{0:.{1}f}'.format(p, n) '3.1416' the nested {1} takes the second argument, the current value of n, and applies it as specified (here, to the "precision" part of the format -- number of digits after the decimal point), and the outer resulting {0:.4f} then applies.