{"id":16174,"date":"2021-08-12T09:37:12","date_gmt":"2021-08-12T04:07:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=16174"},"modified":"2021-11-22T18:38:26","modified_gmt":"2021-11-22T13:08:26","slug":"python-program-to-add-trailing-zeros-to-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-add-trailing-zeros-to-string\/","title":{"rendered":"Python Program to Add Trailing Zeros to String"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Calculate GST<\/a> ljust() method :<\/strong><\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to add trailing Zeros to the given String<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\n
\nGiven a String, and the task is to add the given number of trialing Zeros to the given String.<\/p>\nGiven String = hello btechgeeks\r\nno of zeros to be added = 5<\/pre>\n
The above given string = hello btechgeeks\r\nThe above Given string with added given number of trailing zeros = hello btechgeeks00000<\/pre>\n
Given String = good morning btechgeeks\r\nno of zeros to be added = 10<\/pre>\n
Enter some Random String = good morning btechgeeks\r\nThe above given string = good morning btechgeeks\r\nEnter some Random Number = 10\r\nThe above Given string with added given number of trailing zeros = good morning btechgeeks0000000000<\/pre>\n
Program to Add Trailing Zeros to String<\/h2>\n
Method #1: Using ljust() Method (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_strng = 'hello btechgeeks'\r\n# Print the above given string .\r\nprint(\"The above given string = \" + str(gvn_strng))\r\n# Give the number of trailing zeros to be added as static input and store it in another\r\n# variable.\r\nNo_trzers = 5\r\n# Calculate the length of the above given string using built- in len() function .\r\n# Add it with the given number of zeros and store it in another variable.\r\ntot_lenth = No_trzers + len(gvn_strng)\r\n# Add the given number of trailing zeros to the above given String Using built-in ljust()\r\n# method and store it in another variable.\r\nfinl_reslt = gvn_strng.ljust(tot_lenth, '0')\r\n# Print the above Given string with added given number of trailing Zeros.\r\nprint(\"The above Given string with added given number of trailing zeros = \", finl_reslt)\r\n<\/pre>\n
The above given string = hello btechgeeks\r\nThe above Given string with added given number of trailing zeros = hello btechgeeks00000<\/pre>\n
Method #2 : Using ljust() Method (User Input)<\/h3>\n
\n
# Give the string as User input using\u00a0the\u00a0input()\u00a0function and store it in a variable.\r\ngvn_strng = input(\"Enter some Random String = \")\r\n# Print the above given string .\r\nprint(\"The above given string = \" + str(gvn_strng))\r\n# Give the number of trailing zeros to be added as User input using the int(input()) function and store it in another\r\n# variable.\r\nNo_trzers = int(input(\"Enter some Random Number = \"))\r\n# Calculate the length of the above given string using built- in len() function .\r\n# Add it with the given number of zeros and store it in another variable.\r\ntot_lenth = No_trzers + len(gvn_strng)\r\n# Add the given number of trailing zeros to the above given String Using built-in ljust()\r\n# method and store it in another variable.\r\nfinl_reslt = gvn_strng.ljust(tot_lenth, '0')\r\n# Print the above Given string with added given number of trailing Zeros.\r\nprint(\"The above Given string with added given number of trailing zeros = \", finl_reslt)\r\n<\/pre>\n
Enter some Random String = good morning btechgeeks\r\nThe above given string = good morning btechgeeks\r\nEnter some Random Number = 10\r\nThe above Given string with added given number of trailing zeros = good morning btechgeeks0000000000<\/pre>\n
\n