Python Ljust String Method

Published: 30 September 2015
on channel: Code master
4,003
36

Be sure to like, share and comment to show your support for our tutorials.

=======================================
Channel - https://goo.gl/pnKLqE
Playlist For This Tutorial - https://goo.gl/EyZFti
Latest Video - https://goo.gl/atWRkF
Facebook -   / mastercodeonline  
Twitter - https://twitter.com/mastercodeonlin?l...
Website - http://mastercode.online
======================================
Python Ljust String Method
In this Python tutorial, we will look at the Python ljust string method which will left justify a string string object. The ljust string method takes two arguments. The first argument is how many index positions the new string object should span and the second argument is the separator which must be in a string format.

Python Ljust String Method Syntax
'String Object'.ljust(index, 'separator')

'String Object' - This is our string object that will be left justified
.ljust(index, 'separator') - Ljust string method takes two arguments first one being how many index positions do you want your new string object to span and the second is the separator which will hold the new index positions.
Examples Of The Python Ljust String Method
#Example 1
'This is a string'.ljust(50)
'This is a string '

#Example 2
'This is a string'.ljust(50, '+')
'This is a string++++++++++++++++++++++++++++++++++'

#Example 3
'This is a string'.ljust(17, '!')
'This is a string!'
Examples Explained

Example 1:

'This is a string'.ljust(50) - We create a string object and call the ljust string method on the string object. The string method takes one argument of 50 which is the number of index positions you would like a string to expand.

'This is a string ' - We are returned a new string that contains 50 index positions with the previous string method positioned to the left.

Example 2:

'This is a string'.ljust(50, '+') - We create a string object and call the ljust string method on our string object. Our string method takes two arguments first is 50 index positions and second is the '+' symbol which will replace our default spaces to the right of our new string object.

'This is a string++++++++++++++++++++++++++++++++++' - We are returned a string that is left justified and the default spaces are replaced by '+'.

Example 3:

'This is a string'.ljust(17, '!') - We create a string object and call the ljust string method on our string object. Our string method takes two arguments. The first argument is 17 index positions and the second is the '!' which will replace the default space with a '!'.
'This is a string!' - We are returned a string with a '!' at the end.

*Note - If we provide an argument that is less than the length of the string then we are returned just the string.

Conclusion

In this Python tutorial we took a look at the Python ljust string method. If you have any questions about the ljust string method leave a comment below and we will help you.


On this page of the site you can watch the video online Python Ljust String Method with a duration of hours minute second in good quality, which was uploaded by the user Code master 30 September 2015, share the link with friends and acquaintances, this video has already been watched 4,003 times on youtube and it was liked by 36 viewers. Enjoy your viewing!