Python String ljust() Method

The ljust() method is a string method that returns a left-justified string within a given minimum width. If a fill character is specified, it also fills the remaining space with that character. If the width is less than or equal to the length of the string, it returns the original string.

Here are some examples of using the ljust() method:

# example 1: left justify a string with spaces
string = "cat"
width = 5
print(string.ljust(width))
# cat

# example 2: left justify a string with dashes
string = "Hi"
width = 4
print(string.ljust(width, "-"))
# Hi--

# example 3: left justify a string with no change
string = "banana"
width = 3
print(string.ljust(width))
# banana

If you have any questions about this code, you can drop a line in comment.

 

Comments

Popular posts from this blog

Python chr() Built in Function

Stock Market Predictions with LSTM in Python

Collections In Python

Python Count Occurrence Of Elements

Python One Liner Functions