• 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 / Using Python to Convert Integer to String with Leading Zeros

Using Python to Convert Integer to String with Leading Zeros

June 5, 2022 Leave a Comment

To convert an integer to a string with leading zeros in Python, the easiest way is with the str() function and + operator.

integer = 123

print("000" + str(integer))

#Output:
000123

You can also use the Python string rjust() function to add leading zeros to a number after converting it to a string.

integer = 123

print(str(integer).rjust(6,"0"))

#Output:
000123

One last way is with the format() function.

integer = 123

print("{:>06}".format(str(integer)))

#Output:
000123

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

One such situation is when you have a integer and want to convert it to a string and leading zeros to the string.

Many times, when you are working with data which has an ID or a record number which is represented with a number, you need to add leading zeros to maintain data integrity.

To add leading zeros to a string in Python, the easiest way is with +. In this case, you aren’t worried about length, you know how many zeros you want, and you just want to add leading zeros to your string.

Below is an example showing you how to add leading zeros with string concatenation in Python.

integer = 123

print("000" + str(integer))

#Output:
000123

Using rjust() Function to Add Leading Zeros to String in Python

Another way you can convert an integer to a string and add leading zeros in Python is with the rjust() function.

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.

Adding leading zeros with rjust() is more 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 leading zeros to a string that is a number with rjust() in Python.

integer = 123

print(str(integer).rjust(6,"0"))

#Output:
000123

Using format() Function to Add Leading Zeros to String in Python

One last way you can convert an integer to a string and add leading zeros in Python is with the Python string format() function.

The correct format to use has the form “{:>0N}” where N is the length of the new string you want to create, ‘0’ is for zero, and ‘>’ is for leading.

Using this method is similar to the method with rjust() because here you could easily add leading zeros to a collection of integers after string conversion to make all of the strings have the same length.

Below is the example showing you how to use format() in Python to add leading zeros to an integer after converting it to a string.

integer = 123

print("{:>06}".format(str(integer)))

#Output:
000123

Hopefully this article has been useful for you to learn how to convert an integer to a string and add leading zeros in Python.

Other Articles You'll Also Like:

  • 1.  Create Empty List in Python
  • 2.  pandas variance – Compute Variance of Variables in DataFrame
  • 3.  Write Integer to File Using Python
  • 4.  How to Repeat a Function in Python
  • 5.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 6.  Selenium minimize_window() Function to Minimize Window in Python
  • 7.  Golden Ratio Constant phi in Python
  • 8.  Convert pandas Series to List in Python
  • 9.  Get pandas Series Last Element in Python
  • 10.  pandas fillna – Replace NaN in Dataframe using Python

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