Python Ljust String Method

Veröffentlicht am: 30 September 2015
auf dem Kanal: 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.


Auf dieser Seite können Sie das Online-Video Python Ljust String Method mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Code master 30 September 2015 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 4,003 Mal angesehen und es wurde von 36 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!