程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python string alignment methods (ljust(), rjust(), and center())

編輯:Python

List of articles

  • Python String alignment method
    • Python ljust() Method
    • Python rjust() Method
    • Python center() Method


Python String alignment method

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 .

Python ljust() Method

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 :

  • S: Represents the string to be filled ;
  • width: Means to include S The length itself is included , The total length of the string ;
  • fillchar: As an optional parameter , Used to specify the character used to fill a string , By default, spaces are used .

【 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 ‘-’.

Python rjust() Method

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/

Python center() Method

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/------


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved