DEV Community

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

Posted on • Edited on

String in Python (10)

Buy Me a Coffee

*Memos:

center() can center the string set width as shown below:

*Memos:

  • The 1st argument is width(Required-Type:int): *Memos:
    • It decides the width of a string.
    • Don't use width=.
  • The 2nd argument is fillbyte(Optional-Defualt:" "-Type:str): *Memos:
    • It's the character added to the left and right side of the string set width.
    • It must be one character.
    • Don't use fillbyte=.
v = "hello world" print('"' + v.center(20) + '"') print('"' + v.center(20, " ") + '"') # " hello world " # ↑↑↑↑ ↑↑↑↑↑  print('"' + v.center(20, "?") + '"') # "????hello world?????"  print('"' + v.center(17, "?") + '"') # "???hello world???"  print('"' + v.center(14, "?") + '"') # "?hello world??"  print('"' + v.center(13, "?") + '"') # "?hello world?"  print('"' + v.center(12, "?") + '"') # "hello world?"  print('"' + v.center(11, "?") + '"') print('"' + v.center(10, "?") + '"') print('"' + v.center(0, "?") + '"') # "hello world" 
Enter fullscreen mode Exit fullscreen mode
v = "hello world " # ↑↑↑ print('"' + v.center(14) + '"') print('"' + v.center(0) + '"') # "hello world "  # ↑↑↑ print('"' + v.center(20) + '"') # " hello world " # ↑↑↑ ↑↑↑↑↑↑ 
Enter fullscreen mode Exit fullscreen mode
v = " hello world" # ↑↑↑ print('"' + v.center(14) + '"') print('"' + v.center(0) + '"') # " hello world" # ↑↑↑  print('"' + v.center(20) + '"') # " hello world " # ↑↑↑↑↑↑ ↑↑↑ 
Enter fullscreen mode Exit fullscreen mode

ljust() can left-justify the string set width as shown below:

*Memos:

  • The 1st argument is width(Required-Type:int): *Memos:
    • It decides the width of a string.
    • Don't use width=.
  • The 2nd argument is fillbyte(Optional-Defualt:" "-Type:str): *Memos:
    • It's the character added to the right side of the string set width.
    • It must be one character.
    • Don't use fillbyte=.
v = "hello world" print('"' + v.ljust(20) + '"') print('"' + v.ljust(20, " ") + '"') # "hello world " # ↑↑↑↑↑↑↑↑↑  print('"' + v.ljust(20, "?") + '"') # "hello world?????????"  print('"' + v.ljust(17, "?") + '"') # "hello world??????"  print('"' + v.ljust(14, "?") + '"') # "hello world???"  print('"' + v.ljust(13, "?") + '"') # "hello world??"  print('"' + v.ljust(12, "?") + '"') # "hello world?"  print('"' + v.ljust(11, "?") + '"') print('"' + v.ljust(10, "?") + '"') print('"' + v.ljust(0, "?") + '"') # "hello world" 
Enter fullscreen mode Exit fullscreen mode
v = " hello world" # ↑↑↑ print('"' + v.ljust(14) + '"') print('"' + v.ljust(0) + '"') # " hello world" # ↑↑↑  print('"' + v.ljust(20) + '"') # " hello world " # ↑↑↑ ↑↑↑↑↑↑ 
Enter fullscreen mode Exit fullscreen mode
v = " hello world " # ↑↑↑ ↑↑↑ print('"' + v.ljust(14) + '"') print('"' + v.ljust(0) + '"') # " hello world " # ↑↑↑ ↑↑↑  print('"' + v.ljust(20) + '"') # " hello world " # ↑↑↑ ↑↑↑↑↑↑ 
Enter fullscreen mode Exit fullscreen mode

rjust() can right-justify the string set width as shown below:

*Memos:

  • The 1st argument is width(Required-Type:int): *Memos:
    • It decides the width of a string.
    • Don't use width=.
  • The 2nd argument is fillbyte(Optional-Defualt:" "-Type:str): *Memos:
    • It's the character added to the left side of the string set width.
    • It must be one character.
    • Don't use fillbyte=.
v = "hello world" print('"' + v.rjust(20) + '"') print('"' + v.rjust(20, " ") + '"') # " hello world" # ↑↑↑↑↑↑↑↑↑  print('"' + v.rjust(20, "?") + '"') # "?????????hello world"  print('"' + v.rjust(17, "?") + '"') # "??????hello world"  print('"' + v.rjust(14, "?") + '"') # "???hello world"  print('"' + v.rjust(13, "?") + '"') # "??hello world"  print('"' + v.rjust(12, "?") + '"') # "?hello world"  print('"' + v.rjust(11, "?") + '"') print('"' + v.rjust(10, "?") + '"') print('"' + v.rjust(0, "?") + '"') # "hello world" 
Enter fullscreen mode Exit fullscreen mode
v = "hello world " # ↑↑↑ print('"' + v.rjust(14) + '"') print('"' + v.rjust(0) + '"') # "hello world "  # ↑↑↑  print('"' + v.rjust(20) + '"') # " hello world " # ↑↑↑↑↑↑ ↑↑↑ 
Enter fullscreen mode Exit fullscreen mode
v = " hello world " # ↑↑↑ ↑↑↑ print('"' + v.rjust(14) + '"') print('"' + v.rjust(0) + '"') # " hello world " # ↑↑↑ ↑↑↑  print('"' + v.rjust(20) + '"') # " hello world " # ↑↑↑↑↑↑ ↑↑↑ 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)