Python String rjust() Method

14-01-23 Ahmed Obaid 872 0

​The rjust() function returns a new copy of the original string with the fill characters that are passed in the 'character' argument from the left. If no character is passed in the 'character' argument, blank white spaces will be added. With a number passed to the length argument to specify the total number of characters for the string


How to formulate it like this:


string. adjust(length, character)

character: [Optional] Pass a character to fill in the blanks. If no character is passed, blank spaces will be added by default.

length: A number is passed to specify the total number of characters for the string. If the number passed to this argument is less than or equal to the length of the original string, the original string is returned.

Return value: Returns a new string of a given length and adds certain characters to the left side of the original string.


In the following example, we will not pass any characters or symbols to the "character" argument. By default, the blanks on the left side will be filled with white space. Passing "15" to the length argument to specify the total number of characters for the string

Example:


string = "ahmed"
print(string.rjust(15))

The output will be:


ahmed

Example:


string = "ahmed obaid"
a = string.rjust(20)
print(a, "this is my name.")

The output will be:        


ahmed obaid this is my name.

In the following example, we will pass the $ character to the "character" argument to fill in the blanks from the left. Passing "18" to the length argument to specify the total number of characters for the string

Example:


string = "ahmed obaid"
print(string.rjust(18, '$'))

The output will be:


$$$$$$$ahmed obaid

In the following example, we will pass the underscore "_" to the "character" argument to fill in the blanks from the left. Passing "10" to the length argument to specify the total number of characters for the string

Example:


string = "ahmed"
print(string.rjust(10, '-'))

The output will be:


-----ahmed

External sources:

Built-in functions - official Python documentation

If you have any questions or concerns, leave them in the comments



Tags


Python Python String string methods python python string rjust method

Share page