*Memos:
- My post explains a string.
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=
.
- It's the character added to the left and right side of the string set
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"
v = "hello world " # ↑↑↑ print('"' + v.center(14) + '"') print('"' + v.center(0) + '"') # "hello world " # ↑↑↑ print('"' + v.center(20) + '"') # " hello world " # ↑↑↑ ↑↑↑↑↑↑
v = " hello world" # ↑↑↑ print('"' + v.center(14) + '"') print('"' + v.center(0) + '"') # " hello world" # ↑↑↑ print('"' + v.center(20) + '"') # " hello world " # ↑↑↑↑↑↑ ↑↑↑
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=
.
- It's the character added to the right side of the string set
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"
v = " hello world" # ↑↑↑ print('"' + v.ljust(14) + '"') print('"' + v.ljust(0) + '"') # " hello world" # ↑↑↑ print('"' + v.ljust(20) + '"') # " hello world " # ↑↑↑ ↑↑↑↑↑↑
v = " hello world " # ↑↑↑ ↑↑↑ print('"' + v.ljust(14) + '"') print('"' + v.ljust(0) + '"') # " hello world " # ↑↑↑ ↑↑↑ print('"' + v.ljust(20) + '"') # " hello world " # ↑↑↑ ↑↑↑↑↑↑
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=
.
- It's the character added to the left side of the string set
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"
v = "hello world " # ↑↑↑ print('"' + v.rjust(14) + '"') print('"' + v.rjust(0) + '"') # "hello world " # ↑↑↑ print('"' + v.rjust(20) + '"') # " hello world " # ↑↑↑↑↑↑ ↑↑↑
v = " hello world " # ↑↑↑ ↑↑↑ print('"' + v.rjust(14) + '"') print('"' + v.rjust(0) + '"') # " hello world " # ↑↑↑ ↑↑↑ print('"' + v.rjust(20) + '"') # " hello world " # ↑↑↑↑↑↑ ↑↑↑
Top comments (0)