String in Python (19)

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() (4).

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 's' for str>:

v = 'hello world'

print(v)
# hello world

print('"{:.20s}"'.format(v))
print('"{:.11s}"'.format(v))
print('"{:.11}"'.format(v))
print('"{:s}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "hello world"

print('"{:.9s}"'.format(v))
# "hello wor"

print('"{:.6s}"'.format(v))
# "hello "

print('"{:.2s}"'.format(v))
# "he"

print('"{:.1s}"'.format(v))
# "h"

print('"{:.0s}"'.format(v))
# ""
v = '123456789'

print(v)
# 123456789

print('"{:.15s}"'.format(v))
print('"{:.9s}"'.format(v))
print('"{:.9}"'.format(v))
print('"{:s}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "123456789"

print('"{:.6s}"'.format(v))
# "123456"

print('"{:.2s}"'.format(v))
# "12"

print('"{:.1s}"'.format(v))
# "1"

print('"{:.0s}"'.format(v))
# ""

<Format a string with 'd' for int>:

v = 123456789

print(v)
# 123456789

print('"{:d}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "123456789"

print('"{:,d}"'.format(v))
print('"{:,}"'.format(v))
# "123,456,789"

print('"{:_d}"'.format(v))
print('"{:_}"'.format(v))
# "123_456_789"

<Format a string with 'f' for float>:

v = 1234.5555555555
       # |   10   |
print(v)
# 1234.5555555555
#      |   10   |

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

print('"{:.14f}"'.format(v))
print('"{:.14F}"'.format(v))
# "1234.55555555549995"
#       |     14     |

print('"{:.13f}"'.format(v))
print('"{:.13F}"'.format(v))
# "1234.5555555555000"
#       |    13     |

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

print('"{:.6f}"'.format(v))
print('"{:.6F}"'.format(v))
print('"{:f}"'.format(v))
print('"{:F}"'.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.55555555549995006004"
#        |        20        |

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

print('"{:_f}"'.format(v))
print('"{:_F}"'.format(v))
# "1_234.555556"
#        | 6  |

print('"{:_}"'.format(v))
# "1_234.5555555555"
#        |   10   |
print("{:f} {:f}".format(float('nan'), float('inf')))
# nan inf

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


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:25:31+00:00) String in Python (19). Retrieved from https://www.scien.cx/2025/07/12/string-in-python-19/

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

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.