Python ljust() Method: Left-Justify Strings with Padding
The 'ljust()' method in Python is used to left-justify a string. It takes an argument specifying the width of the resulting string. If the original string is shorter than this width, the method pads the string with spaces (or any other specified character) until it reaches the specified width. If the original string is longer than the width, it truncates the string to fit the specified width.
The syntax of the 'ljust()' method is as follows:
string.ljust(width, fillchar)
Here, 'string' is the original string we want to left-justify, 'width' is the width of the resulting string, and 'fillchar' is the optional character we want to use for padding. If 'fillchar' is not specified, the method uses spaces for padding.
Example:
str = 'Python'
print(str.ljust(10)) # Output: 'Python '
In this example, the original string 'Python' is left-justified with a width of 10 characters. Since the length of the original string is 6, the method pads it with 4 spaces to reach the specified width.
原文地址: https://www.cveoy.top/t/topic/mtSw 著作权归作者所有。请勿转载和采集!