Computer >> Computer tutorials >  >> Programming >> Python

How to get a space-padded string with the original string right-justified in Python?


We can use the method rjust() which returns the string right justified in a string of length equal to 'width' which is the string length in total after padding. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s). For example:

>>> '15'.rjust(10)
'        15'
>>> 'Yes'.rjust(2)
'Yes'
>>> >>> 'raj'.rjust(12)
'         raj'