In the previous article, we have discussed Python Program to Calculate GST
Given a String, and the task is to add the given number of trialing Zeros to the given String.
ljust() method :
We can add trailing zeros to the string using the ljust() method. It has two parameters, x, and y, where x is the length of the string. What will this method accomplish? It will simply go through a string and then to the length of the string that we require, and if both are not the same, it will pad those zeros that they have passed through a string and then store them in another.
Examples:
Example1:
Input:
Given String = hello btechgeeks no of zeros to be added = 5
Output:
The above given string = hello btechgeeks The above Given string with added given number of trailing zeros = hello btechgeeks00000
Example1:
Input:
Given String = good morning btechgeeks no of zeros to be added = 10
Output:
Enter some Random String = good morning btechgeeks The above given string = good morning btechgeeks Enter some Random Number = 10 The above Given string with added given number of trailing zeros = good morning btechgeeks0000000000
Program to Add Trailing Zeros to String
Below are the ways to add trailing Zeros to the given String
Method #1: Using ljust() Method (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Print the above-given string.
- Give the number of trailing zeros to be added as static input and store it in another variable.
- Calculate the length of the above-given string using the built-in len() function.
- Add it with the given number of zeros and store it in another variable.
- Add the given number of trailing zeros to the above-given String Using the built-in ljust() method and store it in another variable.
- Print the above-Given string with added given the number of trailing Zeros.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_strng = 'hello btechgeeks' # Print the above given string . print("The above given string = " + str(gvn_strng)) # Give the number of trailing zeros to be added as static input and store it in another # variable. No_trzers = 5 # Calculate the length of the above given string using built- in len() function . # Add it with the given number of zeros and store it in another variable. tot_lenth = No_trzers + len(gvn_strng) # Add the given number of trailing zeros to the above given String Using built-in ljust() # method and store it in another variable. finl_reslt = gvn_strng.ljust(tot_lenth, '0') # Print the above Given string with added given number of trailing Zeros. print("The above Given string with added given number of trailing zeros = ", finl_reslt)
Output:
The above given string = hello btechgeeks The above Given string with added given number of trailing zeros = hello btechgeeks00000
Method #2 : Using ljust() Method (User Input)
Approach:
- Give the string as User input using the input() function and store it in a variable.
- Print the above given string .
- Give the number of trailing zeros to be added as User input using the int(input()) function and store it in another variable.
- Calculate the length of the above given string using built- in len() function .
- Add it with the given number of zeros and store it in another variable.
- Add the given number of trailing zeros to the above given String Using built-in ljust() method and store it in another variable.
- Print the above Given string with added given number of trailing Zeros.
- The Exit of the program.
Below is the implementation:
# Give the string as User input using the input() function and store it in a variable. gvn_strng = input("Enter some Random String = ") # Print the above given string . print("The above given string = " + str(gvn_strng)) # Give the number of trailing zeros to be added as User input using the int(input()) function and store it in another # variable. No_trzers = int(input("Enter some Random Number = ")) # Calculate the length of the above given string using built- in len() function . # Add it with the given number of zeros and store it in another variable. tot_lenth = No_trzers + len(gvn_strng) # Add the given number of trailing zeros to the above given String Using built-in ljust() # method and store it in another variable. finl_reslt = gvn_strng.ljust(tot_lenth, '0') # Print the above Given string with added given number of trailing Zeros. print("The above Given string with added given number of trailing zeros = ", finl_reslt)
Output:
Enter some Random String = good morning btechgeeks The above given string = good morning btechgeeks Enter some Random Number = 10 The above Given string with added given number of trailing zeros = good morning btechgeeks0000000000
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.