Python str Provides 3 There are two ways to align text , Namely ljust()、rjust() and center() Method , This section will introduce their usage one by one .
ljust() The function of the method is to fill the right side of the specified string with the specified character , So as to achieve the purpose of left aligned text .
ljust() The basic format of the method is as follows :
S.ljust(width[, fillchar])
The meaning of each parameter is as follows :
【 example 1】
S = 'https://editor.csdn.net/md?not_checkout=1&articleId=125648923'
addr = 'https://editor.csdn.net/'
print(S.ljust(35))
print(addr.ljust(35))
The output is :
https://editor.csdn.net/md?not_checkout=1&articleId=125648923
https://editor.csdn.net/
Be careful , In addition to the obviously visible URL string in the output result , There is a space character after it , Each line has a total of 35 Character length .
【 example 2】
S = 'https://editor.csdn.net/md?not_checkout=1&articleId=125648923'
addr = 'https://editor.csdn.net/'
print(S.ljust(35,'-'))
print(addr.ljust(35,'-'))
The output is :
https://editor.csdn.net/md?not_checkout=1&articleId=125648923-----
https://editor.csdn.net/t-------------
This program and example 1 The only difference is , The padding character is changed from space to ‘-’.
rjust() and ljust() The method is similar to , The only difference is ,rjust() The method is to fill the left side of the string with the specified character , So as to achieve the purpose of right aligned text .
rjust() The basic format of the method is as follows :
S.rjust(width[, fillchar])
The meaning of each parameter and ljust() Exactly the same , So I won't repeat the description here .
【 example 3】
S = 'https://editor.csdn.net/md?not_checkout=1&articleId=125648923'
addr = 'https://editor.csdn.net/'
print(S.rjust(35))
print(addr.rjust(35))
The output is :
https://editor.csdn.net/md?not_checkout=1&articleId=125648923
https://editor.csdn.net/
You can see , Each line of string takes 35 Byte position , The overall right alignment effect is realized .
【 example 4】
S = 'https://editor.csdn.net/md?not_checkout=1&articleId=125648923'
addr = 'https://editor.csdn.net/'
print(S.rjust(35,'-'))
print(addr.rjust(35,'-'))
The output is :
-----https://editor.csdn.net/md?not_checkout=1&articleId=125648923
-------------https://editor.csdn.net/
center() String method and ljust() and rjust() The usage of is similar to , But it centers the text , Not left or right .
center() The basic format of the method is as follows :
S.center(width[, fillchar])
The meaning of each parameter and ljust()、rjust() In the same way .
【 example 5】
S = 'https://editor.csdn.net/md?not_checkout=1&articleId=125648923'
addr = 'https://editor.csdn.net/'
print(S.center(35,))
print(addr.center(35,))
The output is :
https://editor.csdn.net/md?not_checkout=1&articleId=125648923
https://editor.csdn.net/
【 example 6】
S = 'https://editor.csdn.net/md?not_checkout=1&articleId=125648923'
addr = 'https://editor.csdn.net/'
print(S.center(35,'-'))
print(addr.center(35,'-'))
The output is :
—https://editor.csdn.net/md?not_checkout=1&articleId=125648923–
-------https://editor.csdn.net/------