Home / Python / Reference / String methods

Python string center() method

Centre a string in a field of a given width.

Syntax

str.center(width, fillchar=" ")

ljust and rjust align left and right. f-strings do the same with :^, :< and :>.

Parameters

widthtotal width
fillcharthe padding character

Return value

str

Example

Code

print("|" + "title".center(15) + "|")
print("menu".center(20, "*"))
print("|" + "left".ljust(10) + "|" + "right".rjust(10) + "|")

Output

|     title     |
********menu********
|left      |     right|

Related

Learn it step by step in the free lesson Strings, with exercises that check your answers.