DEV Community

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

Posted on • Edited on

String in Python (18)

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:

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

*It's like rjust().

v = "John Smith"

print('"{:>20}"'.format(v))
print('"{: >20}"'.format(v))
# "          John Smith"
#  ↑↑↑↑↑↑↑↑↑↑

print('"{:?>20}"'.format(v))
# "??????????John Smith"

print('"{:?>17}"'.format(v))
# "???????John Smith"

print('"{:?>14}"'.format(v))
# "????John Smith"

print('"{:?>13}"'.format(v))
# "???John Smith"

print('"{:?>12}"'.format(v))
# "??John Smith"

print('"{:?>11}"'.format(v))
# "?John Smith"

print('"{:?>10}"'.format(v))
print('"{:?>0}"'.format(v))
print('"{:?>}"'.format(v))
print('"{:>}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "John Smith"
Enter fullscreen mode Exit fullscreen mode
v = "   John Smith   "
   # ↑↑↑          ↑↑↑

print('"{:?>0}"'.format(v))
# "   John Smith   "
#  ↑↑↑          ↑↑↑

print('"{:?>20}"'.format(v))
# "????   John Smith   "
#      ↑↑↑          ↑↑↑
Enter fullscreen mode Exit fullscreen mode

<Right-align a string with '='>:

*It's like zfill().

v = 1234

print('"{:=10}"'.format(v))
print('"{: =10}"'.format(v))
print('"{:= 10}"'.format(v))
print('"{: = 10}"'.format(v))
print('"{:=-10}"'.format(v))
# "      1234"
#  ↑↑↑↑↑↑

print('"{:=+10}"'.format(v))
print('"{:=+10}"'.format(v))
# "+     1234"
#   ↑↑↑↑↑

print('"{:=+010}"'.format(v))
print('"{:0=+10}"'.format(v))
# "+000001234"

print('"{:=+08}"'.format(v))
print('"{:0=+8}"'.format(v))
# "+0001234"

print('"{:=+06}"'.format(v))
print('"{:0=+6}"'.format(v))
# "+01234"

print('"{:=+05}"'.format(v))
print('"{:0=+5}"'.format(v))
print('"{:=+00}"'.format(v))
print('"{:0=+0}"'.format(v))
print('"{:=+0}"'.format(v))
print('"{:0=+}"'.format(v))
print('"{:=+}"'.format(v))
# "+1234"

print('"{:=0}"'.format(v))
print('"{:0=}"'.format(v))
print('"{:=}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "1234"
Enter fullscreen mode Exit fullscreen mode
v = -1234

print('"{: =10}"'.format(v))
print('"{:= 10}"'.format(v))
print('"{: = 10}"'.format(v))
print('"{:=+10}"'.format(v))
print('"{:=-10}"'.format(v))
# "-     1234"

print('"{:=-010}"'.format(v))
print('"{:0=-10}"'.format(v))
# "-000001234"

print('"{:=-08}"'.format(v))
print('"{:0=-8}"'.format(v))
# "-0001234"

print('"{:=-06}"'.format(v))
print('"{:0=-6}"'.format(v))
# "-01234"

print('"{:=-05}"'.format(v))
print('"{:0=-5}"'.format(v))
print('"{:=-00}"'.format(v))
print('"{:0=-0}"'.format(v))
print('"{:=-}"'.format(v))
print('"{:0=-}"'.format(v))
print('"{:=}"'.format(v))
print('"{:0=}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "-1234"
Enter fullscreen mode Exit fullscreen mode

<Make a negative zero a positive zero>:

print(-0.0)                # -0.0
print('{:z}'.format(-0.0)) # 0.0

print(-0.0-0.0j)                # (-0+0j)
print('{:z}'.format(-0.0-0.0j)) # (0+0j)

print(0.0)
print('{:z}'.format(0.0))
# 0.0

print(-0.0+0.0j)
print('{:z}'.format(-0.0+0.0j))
print(0.0-0.0j)
print('{:z}'.format(0.0-0.0j))
# 0j
Enter fullscreen mode Exit fullscreen mode

Top comments (0)