Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




ljust( ) FUNCTION


ljust( ) Function


The ljust() Function is used to left justify a String using Space by default.


Example :


x = "Hello"
y = x.ljust(10)
print(y,"World")


Output :



  Hello   World

In the above code, we have declared a String 'Hello' and assigned it to a variable 'x'.


x = "Hello"

java_Collections

Then we have created a variable 'y' that will reserve '10' blocks. And left justified the String 'Hello' using 'ljust( )' Function.


y = x.ljust(10)

That shifts the String Hello towards the left filling the last 5 locations with space (i.e. ' ').


java_Collections

And if you see the print statement,


print(y,"World")

There is a gap of 5 spaces between 'Hello' and 'World'.


Output :



  Hello   World

Now, let us say you want the fill the right blocks with something other than spaces.


And luckily, Python provides a second parameter that can be used as a padding character.


Let us see with the below example.


Example :


x = "Hello"
y = x.ljust(10, "*")
print(y, "World")


Output :



  Hello***** World

In the above code, we have declared a String 'Hello' and assigned it to a variable 'x'.


x = "Hello"

java_Collections

Then we have created a variable 'y' that will reserve '10' blocks. And left justified the String 'Hello' using 'ljust( )' Function.


y = x.ljust(10, "*")

That shifts the String Hello towards the left filling the last 5 locations with space (i.e. ' ').


java_Collections

And if you see the print statement,


print(y,"World")

There is a gap of 5 spaces between 'Hello' and 'World'.


Output :



  Hello***** World