Add trailing Zeros to string-Python
Last Updated :
05 Apr, 2025
Add trailing zeros to string in Python involves appending a specific number of zero characters ('0') to the end of a given string. This operation is commonly used in formatting, padding, or aligning strings for display or data processing. For example, adding 4 zeros to "GFG" should result in "GFG0000". Let’s explore some efficient approaches to achieve this in Python.
Using string multiplication
This is the most efficient way to add trailing zeros. By multiplying the string '0' with N, we get a string of N zeros. This result is then concatenated to the original string using +. It's concise, easy to understand and fast even for large values of N.
Example:
Python
s = 'GFG'
N = 4 # count
res = s + '0' * N # append
print(res)
Explanation: This first creates a string of N zeros using '0' * N, then joins it with the original string s using +. This gives a new string with zeros added at the end.
Using f-strings
f-Strings offer a modern way to format strings. They support embedding expressions directly inside curly braces {}. Here, '0' * N is evaluated and appended to the string s in one go. This method is especially useful when you have multiple dynamic parts.
Python
s = 'GFG'
N = 4 # count
res = f"{s}{'0' * N}" # append
print(res)
Explanation: This code uses an f-string to combine the original string s with N zeros. Inside the curly braces, '0' * N creates a string of N zeros, which is directly added to the end of s. This results in a new string with zeros added at the end.
Using .ljust()
ljust(width, fillchar) method pads a string to a desired width. In this case, we pad the string s to len(s) + N using '0' as the fill character. This is a great option when you want to pad text for formatting or alignment. It’s clean and avoids manual multiplication or loops.
Python
s = 'GFG'
N = 4 # count
res = s.ljust(len(s) + N, '0') # pad
print(res)
Explanation: ljust() method pad the original string s with zeros on the right. The total length after padding becomes len(s) + N, and '0' is used as the padding character. This adds N zeros at the end of the string.
Using loop
This method manually appends '0' to the string in each iteration. Although easy to understand for beginners, it’s less efficient due to string immutability. Each concatenation creates a new string object, making it slower for large N. Good for educational purposes but not optimal for production use.
Python
s = 'GFG'
N = 4 # count
for _ in range(N): # loop
s += '0'
print(s)
Explanation: This code loops to add one '0' to the string s in each iteration and it runs N times, so '0' is added N times to the end of the string, resulting in a new string with N trailing zeros.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read