Right justify a Python string during formatting
Right justify a string
We can format a string by making a string right justify with rjust method. rjust method has the following syntax.
s.rjust(width [, fill])
Right-justifies string s in a field of the given width; pads on left with character fill (which defaults to a space). The String formatting expression and method can achieve similar effects.
string1 = "Now I am here."
# ww w .j a va 2 s. c om
print string1.center( 50 )
print string1.rjust( 50 )
print string1.ljust( 50 )
hexStr = "java2s.com"
print "Hex String: " + hexStr.upper().rjust(50,'0')
chapters = {1:5, 2:46, 3:52, 4:87, 5:90}
print
for x in chapters:
print "Chapter " + str(x) + str(chapters[x]).rjust(15,'.')
The code above generates the following result.