Introduction
Hello, readers. In this article, we will learn about rjust in Python. rjust is a method that is defined under the String class. Strings are sequences of characters that are represented by single or double quotes. Strings in Python are collections of bytes that represent Unicode characters.
Here’s an interesting thing about Python. Did you know that there is no such thing as a character data type in Python? A single character in Python is just a string of length 1! To access the string's elements, use square brackets.
Also see, Intersection in Python, and Convert String to List Python
rjust in Python
rjust in Python is a method defined under the String class. Rjust in Python right-justifies the string. This means that the method will prepend the given string with the passed “character”. The length of the resultant string would be equal to the length passed in the argument. In case the length of the string is already greater than the passed length limit, it will not prepend.
But, Why do we use rjust in Python?
Just like the rjust() method, there is ljust() method too. These methods are just formatter to justify the content.
Let’s understand all these with examples.
Syntax
The syntax of rjust in Python:
string_name.rjust(length_limit, fill_char)
-
length_limit: It is used to limit the length of the resultant string.
- fill_char: You can pass any “character” here. The method will prepend the string with that. Note that fill_char is an optional parameter. If not passed, the default value would be ‘ ‘(space).
Return Value
Rjust in Python returns the modified string, i.e., the data type of the returned value is String.