rjust() method of str class

The method rjust() of str class returns a right justified string within a string of specified width.If fillchar is not specified whitespace is assumed.

Str Updated 13 Mar 2017 1 min read

Method Name

rjust

Method Signature

rjust(width[, fillchar])

Method Overview

  • The method rjust() returns a new string that is right justified within a string of width 'w'.

  • If the width is less than or equal to the length of the string, then the original string is returned.

  • If fillchar parameter is not specified then whitespace is assume to be the fill character.

Example

python
sampleString    = "1000"

rightJustified  = sampleString.rjust(15)

print(rightJustified)

sampleString    = "1,000,000"

rightJustified  = sampleString.rjust(15)

print(rightJustified)

Output

Output
           1000

      1,000,000