Python 字符串 rjust()方法

Python 字符串 rjust()方法

Python rjust() 方法将字符串右对齐,并用填充字符填充其余空格。此方法返回一个新的字符串,右对齐并填充了填充字符。

语法

rjust(width[, fillchar])

参数

  • width:给定字符串的宽度。
  • fillchar:填充字符串中剩余空间的字符。可选。

返回值

返回右对齐的字符串。

让我们看一些rjust()方法的示例,以了解其功能。

示例1

# Python rjust() method example
# Variable declaration
str = 'Deepinout'
# Calling function
str = str.rjust(20)
# Displaying result
print(str)

输出:

           Deepinout

示例2

# Python rjust() method example
# Variable declaration
str = 'Deepinout'
# Calling function
str = str.rjust(15,"$")
# Displaying result
print(str)

输出:

$$$$$$Deepinout

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程