String in Python (20)

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 do alignment or other things for a string as shown below:

<Format a string with 'f' for Decimal()>:

from decimal import Decimal

v = Decimal(value='1234.5555555555')
                      # |   10   |
print(v)
# 1234.5555555555
#      |   10   |

print('"{:.20f}"'.format(v))
print('"{:.20F}"'.format(v))
# "1234.55555555550000000000"
#       |        20        |

print('"{:.15f}"'.format(v))
print('"{:.15F}"'.format(v))
# "1234.555555555500000"
#       |     15      |

print('"{:.10f}"'.format(v))
print('"{:.10F}"'.format(v))
print('"{:f}"'.format(v))
print('"{:F}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "1234.5555555555"
#       |   10   |

print('"{:.6f}"'.format(v))
print('"{:.6F}"'.format(v))
# "1234.555556"
#       | 6  |

print('"{:.2f}"'.format(v))
print('"{:.2F}"'.format(v))
# "1234.56"

print('"{:.1f}"'.format(v))
print('"{:.1F}"'.format(v))
# "1234.6"

print('"{:.0f}"'.format(v))
print('"{:.0F}"'.format(v))
# "1235"

print('"{:#.0f}"'.format(v))
print('"{:#.0F}"'.format(v))
# "1235."

print('"{:,.20f}"'.format(v))
print('"{:,.20F}"'.format(v))
# "1,234.55555555550000000000"
#        |        20        |

print('"{:,.0f}"'.format(v))
print('"{:,.0F}"'.format(v))
# "1,235"

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

print('"{:f} {:f}"'.format(Decimal(value='nan'), Decimal(value='inf')))
print('"{:F} {:F}"'.format(Decimal(value='nan'), Decimal(value='inf')))
# "NaN Infinity"

*Decimal() without str gets an improper value.

from decimal import Decimal

v = Decimal(value=1234.5555555555)
                     # |   10   |
print(v)
# 1234.555555555499950060038827359676361083984375
#      |                  42                    |

print('"{:.20f}"'.format(v))
print('"{:.20F}"'.format(v))
# "1234.55555555549995006004"
#       |        20        |

print('"{:.15f}"'.format(v))
print('"{:.15F}"'.format(v))
# "1234.555555555499950"
#       |     15      |

print('"{:.10f}"'.format(v))
print('"{:.10F}"'.format(v))
# "1234.5555555555"
#       |   10   |

print('"{:.6f}"'.format(v))
print('"{:.6F}"'.format(v))
# "1234.555556"
#       | 6  |

print('"{:.2f}"'.format(v))
print('"{:.2F}"'.format(v))
# "1234.56"

print('"{:.1f}"'.format(v))
print('"{:.1F}"'.format(v))
# "1234.6"

print('"{:.0f}"'.format(v))
print('"{:.0F}"'.format(v))
# "1235"

print('"{:#.0f}"'.format(v))
print('"{:#.0F}"'.format(v))
# "1235."

print('"{:f}"'.format(v))
print('"{:F}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "1234.555555555499950060038827359676361083984375"
#       |                  42                    |

print('"{:,.20f}"'.format(v))
print('"{:,.20F}"'.format(v))
# "1,234.55555555549995006004"
#        |        20        |

print('"{:,.0f}"'.format(v))
print('"{:,.0F}"'.format(v))
# "1,235"

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

print('"{:f} {:f}"'.format(Decimal(value=float('nan')),
                           Decimal(value=float('inf'))))
print('"{:F} {:F}"'.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-12T03:28:52+00:00) String in Python (20). Retrieved from https://www.scien.cx/2025/07/12/string-in-python-20/

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

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.