DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

String in Python (24)

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 the string set w.
  • a(align) is to align(justify) a string with ^, <, > or =:
    • ^ 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:
    • + indicates that a sign should be used with both positive and negative numbers.
    • - indicates that a sign should be used only with negative numbers, which is the default if s isn't set.
    • " " indicates that a leading space should be used with positive numbers and a minus sign should be used with negative numbers.
  • z converts a negative zero to a positive zero:
    • It can be used only with float or complex input.
  • # converts a string to other form:
    • It can be used only with int, float or complex input.
  • 0 fills the left and/or right side of the padding of the string set w with 0:
    • It's equivalent to the f character 0.
  • w(width) is the width of a string:
    • Padding is added if w is longer than the length of the original string:
      • For ^, padding starts to be added from the right side of a string if w is oddly longer than the length of the original string.
    • It must be 0 <= int if it's set.
    • If it's not set, alignment doesn't occur.
  • g(grouping) separates digits with , or _:
    • It can be used only with int, float or complex input.
    • Decimal() can be used only with ,.
  • .p(.precision) is the scale or precision of a number or the number of the characters of a string:
    • Scale means the number of the digits after the decimal point:
      • Scale is also called decimal part or fractional part.
    • Precision means the total number of the digits before and after the decimal point.
    • For f or F of t(type), scale is decided:
      • Scale is displayed even if they are zero.
    • For g or G of t(type), precision 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) decides 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 int, float, complex or Decimal() input:
    • 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.
      • If .p isn't set, scale is 6 digits for int, float or complex and scale is the original digits for Decimal().
      • If .0 is set, scale is omitted unless # is set.
      • Unnecessary zeros in scale aren't removed.
      • If .p is less than scale, the value is rounded.
    • F is same as f except that uppercase NAN and INF are used.
    • g is general format:
      • If .p is set, precision is .p digits.
      • If .p isn't set, precision is 6 digits for int, float or complex and precision is the original digits for Decimal().
      • .0 is treated as .1.
      • Unnecessary zeros in scale are removed.
      • If .p is less than precision, the value is rounded.
      • If .p is less than integer part(the number of the digits before the decimal point), a scientific notation is used.
    • 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.
    • Decimal() should be used with str to get a proper value.
    • 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(f'"{v:0>-z#010_.0f}"')
print(f'"{v:0>-z#10_.0f}"')
print(f'"{v:>-z#010_.0f}"')
# "000000000."

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

<Center a string with '^'>:

*It's like center().

v = "John Smith"

print(f'"{v:^20}"')
print(f'"{v: ^20}"')
# "     John Smith     "
#  ↑↑↑↑↑          ↑↑↑↑↑

print(f'"{v:?^20}"')
# "?????John Smith?????"

print(f'"{v:?^17}"')
# "???John Smith????"

print(f'"{v:?^14}"')
# "??John Smith??"

print(f'"{v:?^13}"')
# "?John Smith??"

print(f'"{v:?^12}"')
# "?John Smith?"

print(f'"{v:?^11}"')
# "John Smith?"

print(f'"{v:?^0}"')
print(f'"{v:?^}"')
print(f'"{v:^}"')
print(f'"{v:}"')
print(f'"{v}"')
# "John Smith"
Enter fullscreen mode Exit fullscreen mode
v = "   John Smith   "
   # ↑↑↑          ↑↑↑

print(f'"{v:?^0}"')
# "   John Smith   "
#  ↑↑↑          ↑↑↑

print(f'"{v:?^20}"')
# "??   John Smith   ??"
#    ↑↑↑          ↑↑↑
Enter fullscreen mode Exit fullscreen mode

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

*It's like ljust().

v = "John Smith"

print(f'"{v:<20}"')
print(f'"{v: <20}"')
# "John Smith          "
#            ↑↑↑↑↑↑↑↑↑↑

print(f'"{v:?<20}"')
# "John Smith??????????"

print(f'"{v:?<17}"')
# "John Smith???????"

print(f'"{v:?<14}"')
# "John Smith????"

print(f'"{v:?<13}"')
# "John Smith???"

print(f'"{v:?<12}"')
# "John Smith??"

print(f'"{v:?<11}"')
# "John Smith?"

print(f'"{v:?<0}"')
print(f'"{v:?<}"')
print(f'"{v:<}"')
print(f'"{v:}"')
print(f'"{v}"')
# "John Smith"
Enter fullscreen mode Exit fullscreen mode
v = "   John Smith   "
   # ↑↑↑          ↑↑↑

print(f'"{v:?<0}"')
# "   John Smith   "
#  ↑↑↑          ↑↑↑

print(f'"{v:?<20}"')
# "   John Smith   ????"
#  ↑↑↑          ↑↑↑
Enter fullscreen mode Exit fullscreen mode

Top comments (0)