• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python ljust Function – Left Justify String Variable

Python ljust Function – Left Justify String Variable

July 13, 2022 Leave a Comment

The Python ljust() function allows us to left justify string variables.

string = "hello"

print(string.ljust(8))

#Output:
hello   

You can also pass a second parameter which will be used to fill the blank spaces created by ljust().

string = "hello"

print(string.ljust(8, "x"))

#Output:
helloxxx

When working with strings, the ability to easily modify the values of the variables easily is valuable.

One such situation where you might want to make a change to a string is if you want to left justify a string variable.

The Python ljust() function allows us to left justify string variables.

ljust() takes two parameters. The first is the length of the new string that ljust() will create and the second parameter is a fill character to add to the right of the string.

Below is a simple example showing you how to use ljust() to return a left justified string in Python.

string = "hello"

print(string.ljust(8))

#Output:
hello

Using the Fill Parameter with ljust() Function

The second parameter allows you to fill the blank space with a given character. This can be useful if you want to add trailing characters to a string.

Below is a simple example showing you how to return a left justified string with ljust() and fill the blank spaces with a character in Python.

string = "hello"

print(string.ljust(8, "x"))

#Output:
helloxxx

Using ljust() Function to Add Trailing Zeros to String in Python

One useful example of ljust() is that you can add trailing zeros to a string in Python is with the ljust() function.

Adding trailing zeros with ljust() is useful if you want to get a specific length for your new string and don’t always know the length of the string variable you are using.

Below is an example of adding trailing zeros to a string with ljust() in Python.

string = "hello"

print(string.ljust(8,"0"))

#Output:
hello000

Right Justifying String Variable with Python rjust() Function

Instead of left justifying a string, if you want to right justify a string, you can use the Python rjust() function.

Just like ljust(), rjust() takes two parameters. The first is the length of the new string that rjust() will create and the second parameter is the character to add to the left of the string.

Below is a simple example showing you how to use rjust() in Python.

string = "hello"

print(string.rjust(8))

#Output:
   hello

Hopefully this article has been useful for you to learn how to use the Python ljust() function.

Other Articles You'll Also Like:

  • 1.  Using Python to Insert Item Into List
  • 2.  pandas nlargest – Find Largest Values in Series or Dataframe
  • 3.  Using Python to Read Random Line from File
  • 4.  Get First Digit in Number Using Python
  • 5.  Shift Values in a List Using Python
  • 6.  Using Python to Get Home Directory
  • 7.  Keep Every Nth Element in List in Python
  • 8.  Replace Forwardslashes in String Using Python
  • 9.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 10.  Python Random Boolean – How to Generate Random Boolean Values

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy