.ljust()
The method .ljust()
left aligns a string for the given length of characters. If the length of the string is less than the parameter given, then padding is added to the right of the given string. The user can specify a character to use as the padding. The number of padding characters added is the length of the string provided subtracted from the length specified in the method call.
Syntax
string.ljust(length, fillchar)
Required Parameters
string
: The string to be modified with padding.length
: An integer used to determine how many characters of padding will be added. The number of padding characters will be equal tolength
minus the length ofstring
.
Optional Parameters
fillchar
: A character to fill the missing space (to the right of the string). The default value is “ “ (space).
Examples
The default value for the parameter fillchar
is a blank space. The original string is retrieved if the length is less than or equal to the given string’s length.
text = 'Hello Contributors'left_aligned_text = text.ljust(20, '!')print(left_aligned_text)
This code will result in the following output:
Hello Contributors!!
Note:
.ljust()
returns a new string and does not modify the original string. Only one specific character can be mentioned to fill the remaining part of the string with the parameterfillchar
.
Codebyte Example
The code below is runnable, change the value of exampleString
, example_length
, and example_character
to test the .ljust()
method.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.