Python String rjust() Method

The Python rjust() method is a string method that right aligns a string by padding it with a specified character (space by default) to a given width. It has the following syntax:

string.rjust(width, fillchar)

where width is the length of the resulting string, fillchar is an optional parameter that specifies the character to fill the padding (default is space).


The rjust() method returns a new string that is right justified with the given width and fillchar.

Here are some examples of using the Python rjust() method:

# Example 1: Right justify a string with spaces
mystr = 'Hello'
result = mystr.rjust(10)
print(result)

# Output: '     Hello'

# Example 2: Right justify a string with dashes
mystr = 'World'
result = mystr.rjust(10, '-')
print(result)

# Output: '-----World'

# Example 3: Right justify a number with zeros
num = 42
result = str(num).rjust(5, '0')
print(result)

# Output: '00042'

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