Advertisement



< Prev
Next >



Python - String ljust() Method



The ljust() string method is used to create a left justified version of the invoked string, padded with a specific character on its right 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 - ljust() method example.



# Creating a string
s = 'Smile each day and keep sadness away!'


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



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



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



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



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




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



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


Output is :


Original string is :  Smile each day and keep sadness away!
Smile each day and keep sadness away!
Smile each day and keep sadness away!########
Smile each day and keep sadness away!XXX
Smile each day and keep sadness away!00000000000000000000000
Smile each day and keep sadness away!@@@@@@@@@@@@@
Smile each day and keep sadness away!........





Please share this article -





< Prev
Next >
< String rjust() Method
String replace() 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

C#, PYTHON and DJANGO


Advertisement