Advertisement



< Prev
Next >



Python - String rjust() Method



The rjust() string method is used to create a right justified version of the invoked string, padded with a specific character on its left side, which is by default a space character.

Note : None of the string methods change the original string and only return a new string which reflects the change performed by the string method, hence the returned string must be stored or used(if needed to reflect the result), otherwise it is lost.




While right justifying the content of the invoked string, we can pad its content with a specific character of our choice, which is by default, a space character. Let us see an example

# Python - rjust() method example.



# Creating a string
s = 'Eat healthy, live healthy!'


# Printing the original string
print('Original string is : ', s)



# Calling  the rjust() method on string object
# which will return a right justified value of length 30, padded with a space on its left.
print(s.rjust(30))



# Calling  the rjust() method on string object
# which will return a right justified value of length 30, padded with # on its left.
print(s.rjust(30, '#'))



# Calling  the rjust() method on string object
# which will return a right justified value of length 40, padded with X on its left.
print(s.rjust(40, 'X'))



# Calling  the rjust() method on string object
# which will return a right justified value of length 60, padded with 0 on its left.
print(s.rjust(60, '0'))




# Calling  the rjust() method on string object
# which will return a right justified value of length 50, padded with @ on its left.
print(s.rjust(50, '@'))



# Calling  the rjust() method on string object
# which will return a right justified value of length 45, padded with @ on its left.
print(s.rjust(45, '.'))


Output is :


Original string is :  Eat healthy, live healthy!
    Eat healthy, live healthy!
####Eat healthy, live healthy!
XXXXXXXXXXXXXXEat healthy, live healthy!
0000000000000000000000000000000000Eat healthy, live healthy!
@@@@@@@@@@@@@@@@@@@@@@@@Eat healthy, live healthy!
...................Eat healthy, live healthy! 





Please share this article -





< Prev
Next >
< String center() Method
String ljust() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement