String in Python (21)

Buy Me a Coffee☕

*Memos:

My post explains
Format Specification Mini-Language with format() (1).

My post explains Format Specification Mini-Language with format() (2).

My post explains Format Specification Mini-Language with format() (3).

My pos…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

*Memos:

:[f][a][s][z][#][0][w][g][.p][t] can format a string as shown below. *Format Specification Mini-Language explains more details:

<Format a string with 'g' or 'G' for float>:

v = 123456.78912
  # |    11    |

print(v)
# 123456.78912
# |    11    |

print('"{:.20g}"'.format(v))
print('"{:.20G}"'.format(v))
# "123456.78912000000128"
#  |        20         |

print('"{:.18g}"'.format(v))
print('"{:.18G}"'.format(v))
# "123456.789120000001"
#  |       18        |

print('"{:.17g}"'.format(v))
print('"{:.17G}"'.format(v))
print('"{:.11g}"'.format(v))
print('"{:.11G}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "123456.78912"
#  |    11    |

print('"{:.10g}"'.format(v))
print('"{:.10G}"'.format(v))
# "123456.7891"
#  |   10    |

print('"{:.9g}"'.format(v))
print('"{:.9G}"'.format(v))
# "123456.789"
#  |   9    |

print('"{:.8g}"'.format(v))
print('"{:.8G}"'.format(v))
# "123456.79"
#  |   8   |

print('"{:.7g}"'.format(v))
print('"{:.7G}"'.format(v))
# "123456.8"
#  |  7   |

print('"{:.6g}"'.format(v))
print('"{:.6G}"'.format(v))
print('"{:g}"'.format(v))
print('"{:G}"'.format(v))
# "123457"
#  | 6  |

print('"{:.5g}"'.format(v))
# "1.2346e+05"
#  | 5  |

print('"{:.5G}"'.format(v))
# "1.2346E+05"
#  | 5  |

print('"{:.4g}"'.format(v))
# "1.235e+05"
#  | 4 |

print('"{:.4G}"'.format(v))
# "1.235E+05"
#  | 4 |

print('"{:.3g}"'.format(v))
# "1.23e+05"
#  |3 |

print('"{:.3G}"'.format(v))
# "1.23E+05"
#  |3 |

print('"{:.2g}"'.format(v))
# "1.2e+05"
#  |2|

print('"{:.2G}"'.format(v))
# "1.2E+05"
#  |2|

print('"{:.1g}"'.format(v))
print('"{:.0g}"'.format(v))
# "1e+05"

print('"{:.1G}"'.format(v))
print('"{:.0G}"'.format(v))
# "1E+05"

print('"{:#.1g}"'.format(v))
print('"{:#.0g}"'.format(v))
# "1.e+05"

print('"{:#.1G}"'.format(v))
print('"{:#.0G}"'.format(v))
# "1.E+05"

print('"{:,.20g}"'.format(v))
print('"{:,.20G}"'.format(v))
# "123,456.78912000000128"
#  |         20         |

print('"{:,}"'.format(v))
# "123,456.78912"
#  |    11     |

print('"{:_.20g}"'.format(v))
print('"{:_.20G}"'.format(v))
# "123_456.78912000000128"
#  |         20         |

print('"{:_}"'.format(v))
# "123_456.78912"
#  |    11     |
print("{:g} {:g}".format(float('nan'), float('inf')))
# nan inf

print("{:G} {:G}".format(float('nan'), float('inf')))
# NAN INF

<Format a string with 'g' or 'G' for Decimal()>:

v = Decimal('123456.78912')
           # |    11    |

print(v)
# 123456.78912
# |    11    |

print('"{:.20g}"'.format(v))
print('"{:.20G}"'.format(v))
print('"{:.18g}"'.format(v))
print('"{:.18G}"'.format(v))
print('"{:.17g}"'.format(v))
print('"{:.17G}"'.format(v))
print('"{:.11g}"'.format(v))
print('"{:.11G}"'.format(v))
print('"{:g}"'.format(v))
print('"{:G}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "123456.78912"
#  |    11    |

print('"{:.10g}"'.format(v))
print('"{:.10G}"'.format(v))
# "123456.7891"
#  |   10    |

print('"{:.9g}"'.format(v))
print('"{:.9G}"'.format(v))
# "123456.789"
#  |   9    |

print('"{:.8g}"'.format(v))
print('"{:.8G}"'.format(v))
# "123456.79"
#  |   8   |

print('"{:.7g}"'.format(v))
print('"{:.7G}"'.format(v))
# "123456.8"
#  |  7   |

print('"{:.6g}"'.format(v))
print('"{:.6G}"'.format(v))
# "123457"
#  | 6  |

print('"{:.5g}"'.format(v))
# "1.2346e+5"
#  | 5  |

print('"{:.5G}"'.format(v))
# "1.2346E+5"
#  | 5  |

print('"{:.4g}"'.format(v))
# "1.235e+5"
#  | 4 |

print('"{:.4G}"'.format(v))
# "1.235E+5"
#  | 4 |

print('"{:.3g}"'.format(v))
# "1.23e+5"
#  |3 |

print('"{:.3G}"'.format(v))
# "1.23E+5"
#  |3 |

print('"{:.2g}"'.format(v))
# "1.2e+5"
#  |2|

print('"{:.2G}"'.format(v))
# "1.2E+5"
#  |2|

print('"{:.1g}"'.format(v))
print('"{:.0g}"'.format(v))
# "1e+5"

print('"{:.1G}"'.format(v))
print('"{:.0G}"'.format(v))
# "1E+5"

print('"{:#.1g}"'.format(v))
print('"{:#.0g}"'.format(v))
# "1.e+5"

print('"{:#.1G}"'.format(v))
print('"{:#.0G}"'.format(v))
# "1.E+5"

print('"{:,.20g}"'.format(v))
print('"{:,.20G}"'.format(v))
print('"{:,}"'.format(v))
# "123,456.78912"
#  |    11     |

print('"{:_.20g}"'.format(v))
print('"{:_.20G}"'.format(v))
print('"{:_}"'.format(v))
# ValueError: invalid format string
from decimal import Decimal

print('"{:g} {:g}"'.format(Decimal(value=float('nan')),
                           Decimal(value=float('inf'))))
print('"{:G} {:G}"'.format(Decimal(value=float('nan')),
                           Decimal(value=float('inf'))))
# "NaN Infinity"


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2025-07-13T03:39:36+00:00) String in Python (21). Retrieved from https://www.scien.cx/2025/07/13/string-in-python-21/

MLA
" » String in Python (21)." Super Kai (Kazuya Ito) | Sciencx - Sunday July 13, 2025, https://www.scien.cx/2025/07/13/string-in-python-21/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Sunday July 13, 2025 » String in Python (21)., viewed ,<https://www.scien.cx/2025/07/13/string-in-python-21/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » String in Python (21). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/13/string-in-python-21/
CHICAGO
" » String in Python (21)." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/07/13/string-in-python-21/
IEEE
" » String in Python (21)." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/07/13/string-in-python-21/. [Accessed: ]
rf:citation
» String in Python (21) | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/07/13/string-in-python-21/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.