String in Python (17)

Buy Me a Coffee☕

*Memos:

My post explains format().

My post explains a string.

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

*Memos

: must be used with f, a, s…


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:

*Memos

  • : must be used with f, a, s, z, #, 0, w, g, .p or t.
  • f(fill) is the zero or one character to fill the left and/or right side of the padding of a string.
  • a(align) is to align a string with ^, <, > or =: *Memos:
    • ^ can center a string.
    • < can left-align(left-justify) a string.
    • > can right-align(right-justify) a string.
    • = can right-align(right-justify) a string only with int or float input. *Padding is added between + or - and input.
  • s(sign) is +, -, or " " which can be used only with int, float or complex input: *Memos:
    • + indicates that a sign should be used for both positive and negative numbers.
    • - indicates that a sign should be used only for negative numbers, which is the default if s isn't set.
    • " " indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers.
  • z is to make a negative zero a positive zero. *z can be used only with float or complex input.
  • # is to convert a string to other form. *# can be used only with int, float or complex input.
  • 0 is to fill the left and/or right side of the one or more available spaces of a string with 0. It's equivalent to the f character 0.
  • w is to decide the width of a string: *Memos:
    • It must be 0 <= integer if it's set.
    • If it's not set, alignment doesn't occur.
  • g(grouping) is to separate digits with , or _. *g can be used only with int, float or complex input.
  • .p(.precision) is to decide the scale or precision of a number or the number of the characters of a string: *Memos:
    • For f or F, scale(the number of the digits after the decimal point) is decided. *Scale is displayed even if they are zero.
    • For g or G, precision(the total number of the digits before and after the decimal point is decided as much as possible. *The digits after the decimal point aren't displayed if they are zero.
    • For s, the number of characters is decided.
  • t(type) is to decide how data must be presented with s for str input, b, c, d, o, x, X or n for int input or e, E, f, F, g, G, n or % for float, complex and Decimal() input: *Memos:
    • s is string format. *If t isn't set for str input, t is default to s.
    • d is decimal integer. *If t isn't set for int input, t is default to d.
    • f is fixed-point notation. *If .p is set, scale is .p digits while if .p isn't set, scale is 6 digits and if .0 is set, scale is omitted unless # is set.
    • F is same as f except that uppercase NAN and INF are used.
    • g is general format.
    • G is same as g except that uppercase E, INFINITY and NAN are used.
    • If t isn't set for float or complex, g-like operation is done.
    • If t isn't set for Decimal(), g or G is set, depending on the value of context.capitals.
    • The doc explains other types like b, c, d, etc.
  • If both f and 0 are set, f is prioritized.
  • If both f and 0 aren't set, the default is " ".
  • f=0 and/or 0 cannot be used with complex input.

<Put everything all together>:

v = -0.0

print('"{:0>-z#010_.0f}"'.format(v))
print('"{:0>-z#10_.0f}"'.format(v))
print('"{:>-z#010_.0f}"'.format(v))
# "000000000."

print('"{0:0>-z#010_.0f}"'.format(v))
print('"{0:0>-z#10_.0f}"'.format(v))
print('"{0:>-z#010_.0f}"'.format(v))
# "000000000."

print('"{:0>-z#010_.0f}" "{:0>-z#10_.0f}" "{:>-z#010_.0f}"'.format(v, v, v))
print('"{0:0>-z#010_.0f}" "{1:0>-z#10_.0f}" "{2:>-z#010_.0f}"'.format(v, v, v))
# "000000000." "000000000." "000000000."

<Center a string with '^'>:

*It's like center().

v = "hello world"

print('"{:^20}"'.format(v))
print('"{: ^20}"'.format(v))
# "    hello world     "
#  ↑↑↑↑           ↑↑↑↑↑

print('"{:?^20}"'.format(v))
# "????hello world?????"

print('"{:?^17}"'.format(v))
# "???hello world???"

print('"{:?^14}"'.format(v))
# "?hello world??"

print('"{:?^13}"'.format(v))
# "?hello world?"

print('"{:?^12}"'.format(v))
# "hello world?"

print('"{:?^11}"'.format(v))
print('"{:?^0}"'.format(v))
print('"{:?^}"'.format(v))
print('"{:^}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "hello world"
v = "hello world   "
              # ↑↑↑

print('"{:?^14}"'.format(v))
print('"{:?^0}"'.format(v))
# "hello world   "
#             ↑↑↑

print('"{:?^20}"'.format(v))
# "???hello world   ???"
#                ↑↑↑
v = "   hello world"
   # ↑↑↑

print('"{:?^14}"'.format(v))
print('"{:?^0}"'.format(v))
# "   hello world"
#  ↑↑↑

print('"{:?^20}"'.format(v))
# "???   hello world???"
#     ↑↑↑

<Left-align a string with '<'>:

*It's like ljust().

v = "hello world"

print('"{:<20}"'.format(v))
print('"{: <20}"'.format(v))
# "hello world         "
#             ↑↑↑↑↑↑↑↑↑

print('"{:?<20}"'.format(v))
# "hello world?????????"

print('"{:?<17}"'.format(v))
# "hello world??????"

print('"{:?<14}"'.format(v))
# "hello world???"

print('"{:?<13}"'.format(v))
# "hello world??"

print('"{:?<12}"'.format(v))
# "hello world?"

print('"{:?<11}"'.format(v))
print('"{:?<0}"'.format(v))
print('"{:?<}"'.format(v))
print('"{:<}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "hello world"
v = "   hello world"
   # ↑↑↑

print('"{:?<14}"'.format(v))
print('"{:?<0}"'.format(v))
# "   hello world"
#  ↑↑↑

print('"{:?<20}"'.format(v))
# "   hello world??????"
#  ↑↑↑
v = "   hello world   "
   # ↑↑↑           ↑↑↑

print('"{:?<14}"'.format(v))
print('"{:?<0}"'.format(v))
# "   hello world   "
#  ↑↑↑           ↑↑↑

print('"{:?<20}"'.format(v))
# "   hello world   ???"
#  ↑↑↑           ↑↑↑


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-11T00:13:01+00:00) String in Python (17). Retrieved from https://www.scien.cx/2025/07/11/string-in-python-17/

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

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.