String ljust() Method in Python

String ljust() Method in Python

The Python programming language provides the user with the ability to easily process strings in a Python program. It provides the ljust() function for working with strings in Python. It inserts additional characters into the string up to the specified length. This guide will walk you through the process of implementing the ljust() function in a Python program. Here you will learn about the operation and syntax of the ljust() function. After studying the examples, you can easily use the ljust() function in your programs.

 ljust() function

ljust means "left justified" - align or set a string completely to the left. The ljust() method pads the given string with extra characters and shifts it completely to the left. It pads the given string with the specified number of characters to make it a certain length.

Here is the syntax for the ljust function used in a typical Python program:

Here is the syntax of ljust function used

The ljust() function takes a minimum of one and a maximum of two input parameters. "String" is the name of the string that will be left justified using the ljust() function. The 'len' parameter represents the desired length of the string after left adjustment. The 'chr' parameter represents the character that will be padded with the string to make it a certain length and left justified. The default is a space » «. If you don't specify any character, the ljust() function will pad the string with spaces from the left until the specified length is reached.

Example 1

In this example, we will provide a string and the desired length of the string after left justification. We will not provide any character here that will be padded with a string in order to test the ljust() function with only the length of the string parameter.

str  =  "MyWorld"

           print ( "The string is 20 characters left justified:" ,  str . ljust ( 20 ) , "." )

As you can see, we put "." at the end of the line after the ljust() function has been applied. This is to help you understand and clearly see the 20-character string filled with spaces. The output of the above program is given below:

as you can see, we have

Note that the '.' is printed after creating a 20-character string and left justified. The ljust() function placed 13 spaces to the right of the given string to left justify the string.

Example 2

Now that we know that if we don't provide a fill character parameter to the ljust() function, it will simply add extra spaces to the string to make it a certain length. Now let's add a fill character parameter to the ljust() function to see how it works. Refer to the code below:

str  =  "MyWorld"

print ( "The string is 20 characters left justified:" ,  str . ljust ( 20 , "b" ) )

The character 'b' is provided as a fill character for the string. The ljust() function will now pad the given string with 'b' until it is 20 characters long. Once we execute the above code, we will get the following output:

The character 'b' is provided as a fill character

As you can see, the given string consists of 7 characters "My World". The specified length is 20 and the fill character is "b". The ljust() function added 13 b's to the string to make it 20 characters long and fully left justified.

Example 3

Now that we understand how ljust() methods work and functionality, let's just test them with different types of strings and give different inputs. Refer to the code below:


Here you can see three values ​​"str = AbCdEfGh, 123456…… Now len = 50 and chr = B". After that, the Ijust function is used for these data types and the result is displayed.


str  =  "AbCdEfGh, 123456……Now"

len  =  50

chr  =  "B"

print ( str . ljust ( len ,  chr ) )


When you run this code, you will get the following output:

When you run this code

Note that ljust() counts the length of the original string and pads the remaining characters with a padding character.

Example 4

The padding character must be 1 character long. If you specify more than one character for the padding character, the compiler will throw a TypeError. In this example, we will provide more than one character for the padding character parameter to see what TypeError will be raised by the compiler. Refer to the code below:

str  =  "AbCdEfGh, 123456……Now"

len  =  50

chr  =  "Airbnb"

print ( str . ljust ( len ,  chr ) )


"chr = AirBnb" consists of 6 characters. So the compiler should throw a TyperError. Let's look at the output below:

As you can see in the output, the compiler raised "TypeError

As you can see in the output, the compiler raised "TypeError: fill character must be exactly one character". This is because you cannot specify more than one character in a padding character. It only allows 1 character length.

Example 5

In this example, we will check how the ljust() function will react if we provide the required length equal to the length of the given string. Please refer to the code below first and then we will discuss it later:

str  =  "MyWorld"

print ( "The string is left justified:" ,  str . ljust ( 7 , "p" ) )

Note that the length of the given string 'MyWorld' is 7, and the required length is also 7. The fill character 'p; is also provided. Now let's look at the result of the ljust() function for this example:

output

Note that despite the presence of a fill character, the ljust() function does not pad the string with it. The ljust() function was ignoring the padding character because the required string length is equal to the length of the original string.

Conclusion

In this article, we saw the ljust() function. This is a built-in function of the python programming language that is used to left justify a string, making it long with certain characters. We have included various scenarios related to this feature.

Please leave your comment to encourage us

Previous Post Next Post