Python 如何对齐文本字符串
在这篇文章中,我们将学习如何使用Python对齐文本字符串。我们将使用 f-strings 来对齐Python中的文本字符串。
Python的文本对齐功能可帮助打印格式良好、整洁的输出。有时要打印的数据长度不同,打印时可能显得不整齐。通过指定对齐方式为左对齐、右对齐或居中对齐,并指定字符串的宽度,可以使用 字符串对齐 来对齐输出字符串。
通过使用 f-strings 来格式化文本字符串。输出字符串的对齐方式由符号 **‘ <’, ‘>’, ‘^’ ** 和后面的宽度数值定义。
字符串的 ljust()、rjust() 和 centre() 方法也可以用来对齐字符串。
使用的方法
- 使用f-strings。
-
使用ljust()、rjust()和centre()方法。
方法1:使用f-strings进行左对齐
指定‘ <
’后跟宽度数字作为 左对齐 输出字符串的语法。
示例
以下程序将文本左对齐,并保留30个空格位−
# 30 spaces are reserved in this case for the specific output string.
# On the left side, the string is printed.
print(f"{'The Left Aligned String!!!' : <30}")
输出
执行上述程序后,将生成以下输出:
The Left Aligned String!!!
方法2:使用f-strings进行右对齐
在‘ >
‘之后指定宽度参数以实现 右对齐 的输出字符串格式。
示例
下面的程序将文本向右对齐,并保留30个空格 −
# 30 spaces are reserved in this case for the specific output string.
# On the right side, the string is printed.
print(f"{'The Right Aligned String!!!' : >30}")
输出
执行上述程序将生成以下输出:
The Right Aligned String!!!
方法3:使用f-strings进行居中对齐
指定 ‘^’ 后跟输出字符串的 居中对齐 的宽度数。
示例
以下程序将文本居中对齐:
# 30 spaces are reserved in this case for the specific output string.
# In the middle, the string is printed.
print(f"{'Center Aligned String!!!' : ^30}")
输出
执行上述程序后,会生成以下输出:
Center Aligned String!!!
方法4:以对齐格式打印变量
示例
以下程序使用f-strings和<,^, >符号分别以3种不同的对齐格式对给定的3个字符串进行对齐。
# input string for all the 3 types of alignments
leftAlign = "Left Alignement"
centerAlign = "Center Alignement"
rightAlign = "Right Alignement"
# printing the resultant aligned text using the <,^, > symbols
print(f"{leftAlign : <25}{centerAlign : ^20}{rightAlign : >25}")
输出
执行上述程序时,将生成以下输出内容 −
Left Alignement Center Alignement Right Alignement
方法5:以对齐的列格式打印多个列表值
示例
以下程序使用f-strings和<,^, >符号将给定的4个列表分别以4个对齐的列格式对齐:
# input lists of multiple columns
cricketers = ['Dhoni', 'Virat Kohli',
'Hardik Pandya', 'KL Rahul', 'Rohit Sharma']
score = [55, 90, 60, 45, 70]
place = ['Ranchi', 'Mumbai', 'Mumbai', 'Mumbai', 'Kolkata']
strikerate = [90, 60, 30, 50, 70]
# Aligning the Headers and printing them
print(f"{'Cricketers' : <20}{'Score' : ^20}{'Place' : ^20}{'Strikerate' : >10}")
# Aligning the variable values and printing them
# looping 4 times as 4 columns are using the for loop
for i in range(0, 4):
# aligning variable values using left, center, and right alignments
# with specific widths
print(
f"{cricketers[i] : <20}{score[i] : ^20}{place[i] : ^20}{strikerate[i] : >10}")
输出
在执行时,上面的程序将生成以下输出−
Cricketers Score Place Strikerate
Dhoni 55 Ranchi 90
Virat Kohli 90 Mumbai 60
Hardik Pandya 60 Mumbai 30
KL Rahul 45 Mumbai 50
方法6:使用ljust()、rjust()和center()方法对齐文本
字符串的ljust()、rjust()和center()方法也可以用于对齐字符串。
ljust()方法
使用指定字符(默认为空格)作为填充字符,将字符串左对齐。
语法
string.ljust(length, character)
参数
- length(必需) - 返回的字符串长度。
-
character(可选) - 用来填充字符串右侧缺失空间的字符。默认为空格(” “)。
rjust() 方法
使用指定的字符(默认为空格)作为填充字符,将字符串右对齐。
语法
string.rjust(length, character)
参数
- length(必需) - 返回的字符串长度
-
character(可选) - 填充字符串左侧缺失空间的字符。空格 (” “) 是默认值。
center() 方法
center() 方法使用指定的字符(默认为空格)作为填充字符,居中对齐字符串。
语法
string.center(length, character)
参数
- length(必需) - 返回的字符串长度
-
character(可选) - 使用指定的字符填充左右两边的空白空间。空格(“ ”)是默认值。
示例
以下程序使用ljust()、rjust()和center()方法分别显示了文本的左对齐、右对齐和居中对齐:
# input text
inputText = 'Tutorialspoint'
# left aligning the text of a width of 30
print("Left Alignment:", inputText.ljust(30))
# right aligning the text of a width of 30
print("Right Alignment:", inputText.rjust(30))
# center aligning the text of a width of 30
print("Center Alignment:", inputText.center(30))
输出
在执行上述程序时,将会生成以下输出结果:
Left Alignment: Tutorialspoint
Right Alignment: Tutorialspoint
Center Alignment: Tutorialspoint
方法7:使用指定字符对齐文本
示例
下面的程序展示了使用ljust()、rjust()和center()方法分别使用指定字符对齐文本的左对齐、右对齐和居中对齐。
# input text
inputText = 'Tutorialspoint'
# left aligning the text of a width of 30
# fills the space with the '@' character
print("Left Alignment:", inputText.ljust(30, '@'))
# right aligning the text of a width of 30
# fills the space with the '#' character
print("Right Alignment:", inputText.rjust(30, '#'))
# center aligning the text of a width of 30
# fills the space with the '%' character
print("Center Alignment:", inputText.center(30, '%'))
输出
在执行过程中,上述程序将生成以下输出 –
Left Alignment: Tutorialspoint@@@@@@@@@@@@@@@@
Right Alignment: ################Tutorialspoint
Center Alignment: %%%%%%%%Tutorialspoint%%%%%%%%
结论
本文演示了如何在Python中使用ljust()、rjust()和centre()方法来对齐文本字符串。此外,我们还学习了如何使用f字符串在不同位置对齐文本字符串。