Python ljust rjust center輸出
更新時間:2008年09月06日 13:25:12 作者:
Python中打印字符串時可以調(diào)用ljust(左對齊),rjust(右對齊),center(中間對齊)來輸出整齊美觀的字符串,使用起來非常簡單,包括使用第二個參數(shù)填充(默認(rèn)為空格)。
看下面的例子就會明白了:
print '|','*'.ljust(10),'|'
print '|','*'.ljust(10,'-'),'|'
print '|','*'.rjust(10,'-'),'|'
print '|','*'.center(10,'-'),'|'
for a in range(1, 6):
print 'a = '.ljust(5), repr(a).ljust(10), 'b = '.ljust(5), repr(a * 2)
輸出結(jié)果:
| * |
| *--------- |
| ---------* |
| ----*----- |
a = 1 b = 2
a = 2 b = 4
a = 3 b = 6
a = 4 b = 8
a = 5 b = 10
復(fù)制代碼 代碼如下:
print '|','*'.ljust(10),'|'
print '|','*'.ljust(10,'-'),'|'
print '|','*'.rjust(10,'-'),'|'
print '|','*'.center(10,'-'),'|'
for a in range(1, 6):
print 'a = '.ljust(5), repr(a).ljust(10), 'b = '.ljust(5), repr(a * 2)
輸出結(jié)果:
| * |
| *--------- |
| ---------* |
| ----*----- |
a = 1 b = 2
a = 2 b = 4
a = 3 b = 6
a = 4 b = 8
a = 5 b = 10
相關(guān)文章
python+influxdb+shell編寫區(qū)域網(wǎng)絡(luò)狀況表
這篇文章主要為大家詳細(xì)介紹了python+influxdb+shell編寫區(qū)域網(wǎng)絡(luò)狀況表,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
使用python讀取csv文件快速插入數(shù)據(jù)庫的實(shí)例
今天小編就為大家分享一篇使用python讀取csv文件快速插入數(shù)據(jù)庫的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
一道題學(xué)會Python函數(shù)中形參和實(shí)參
在Python編程中,函數(shù)的形參和實(shí)參是函數(shù)定義和調(diào)用的重要概念,本文主要介紹了一道題學(xué)會Python函數(shù)中形參和實(shí)參,具有一定的參考價值,感興趣的可以了解一下2024-01-01

