Padding with ljust(),rjust() and center() function in python

ljust()  function in python returns the string with padding done on the right end of the string to the specified length

rjust()  function in python returns the string with padding done on the left end of the string to the specified length. rjust() is opposite of ljust() function

center()  function in python returns the string with padding done on the either end of the string to the specified length

 

syntax of ljust(),  rjust() and center() function in python:

  1. str.ljust(width, fillchar)
  2. str.rjust(width, fillchar)
  3. str.center(width, fillchar)
  • width— This is string length in total after padding.
  • fillchar— This is filler character used for padding, default is a space.

 

Example of ljust() Function in python:

ljust() function in python pads the string in the end with fillchar

# ljust() function in python 

str = "Salute to the mother earth";
print str.ljust(50, '^')

we are using cap symbol to pad the string and we need 50 character string as a result so the output will be

Salute to the mother earth^^^^^^^^^^^^^^^^^^^^^^^^

 

Example of rjust() Function in python:

rjust() function is opposite to ljust() function in python which pads the string in the start with fillchar

# rjust() function in python 

str = "Salute to the mother earth";
print str.rjust(50, '^')

we are using cap symbol to pad the string and we need 50 character string as a result so the output will be

^^^^^^^^^^^^^^^^^^^^^^^^Salute to the mother earth

 

Example of center() Function in python:

center() function in python pads the string on either end equally with fillchar

# center() function in python

str = "Salute to the mother earth";

print str.center(50, '^')

we are using cap symbol to pad the string and we need 50 character string as a result so the output will be

^^^^^^^^^^^^Salute to the mother earth^^^^^^^^^^^^

Padding the columns of dataframe in python can be referred here

previous ljust(),rjust() and center() function in python                                                                                                                next ljust(),rjust() and center() function in python