{"id":18471,"date":"2021-08-26T10:39:07","date_gmt":"2021-08-26T05:09:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=18471"},"modified":"2021-11-22T18:37:16","modified_gmt":"2021-11-22T13:07:16","slug":"python-program-to-sort-digits-of-a-number-in-ascending-order","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-sort-digits-of-a-number-in-ascending-order\/","title":{"rendered":"Python Program to Sort digits of a Number in Ascending Order"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Count the Number of Odd and Even Digits<\/a> join() method in python:<\/strong><\/p>\n The join() method in Python can be used to convert a List to a String.<\/p>\n Iterables such as Lists, Tuples, Strings, and others are accepted as parameters for the join() process.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to get sorted digits in ascending order of a given number after the removal of leading zeros:<\/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 Count the Number of Odd and Even Digits The task is to sort the digits in ascending order given a number N. Print the new number after removing the leading zeroes. join() method in python: The join() method in Python can be used to convert …<\/p>\n
\nThe task is to sort the digits in ascending order given a number N. Print the new number after removing the leading zeroes.<\/p>\nGiven Number = 4561230008<\/pre>\n
The sorted digits in ascending order of a given number 4561230008 after removal of leading zeros =\r\n1234568<\/pre>\n
Given Number = 34879010<\/pre>\n
The sorted digits in ascending order of a given number 34879010 after removal of leading zeros =\r\n134789<\/pre>\n
Program to Sort digits of a Number in Ascending Order in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\nnumb = 4561230008\r\n# Convert the given number into a string number using the str() function and\r\n# store it in another variable.\r\nstrng_numbr = str(numb)\r\n# Sort the above-obtained string number using the sorted() function and store\r\n# it in another variable.\r\nsortdlst_num = sorted(strng_numbr)\r\n# Convert this sorted list of numbers into a string using the join function and\r\n# store it in another variable.\r\nsortd_str = ''.join(sortdlst_num)\r\n# Convert the above obtained sorted string to an integer using the int() function\r\n# (This removes the leading zeros) and store it in another variable.\r\nrslt = int(sortd_str)\r\n# Print the sorted digits in ascending order of a given number after removal of\r\n# leading zeros.\r\nprint(\"The sorted digits in ascending order of a given number\",\r\n numb, \"after removal of leading zeros =\")\r\nprint(rslt)\r\n<\/pre>\n
The sorted digits in ascending order of a given number 4561230008 after removal of leading zeros =\r\n1234568<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\nnumb = int(input(\"Enter some random number = \"))\r\n# Convert the given number into a string number using the str() function and\r\n# store it in another variable.\r\nstrng_numbr = str(numb)\r\n# Sort the above-obtained string number using the sorted() function and store\r\n# it in another variable.\r\nsortdlst_num = sorted(strng_numbr)\r\n# Convert this sorted list of numbers into a string using the join function and\r\n# store it in another variable.\r\nsortd_str = ''.join(sortdlst_num)\r\n# Convert the above obtained sorted string to an integer using the int() function\r\n# (This removes the leading zeros) and store it in another variable.\r\nrslt = int(sortd_str)\r\n# Print the sorted digits in ascending order of a given number after removal of\r\n# leading zeros.\r\nprint(\"The sorted digits in ascending order of a given number\",\r\n numb, \"after removal of leading zeros =\")\r\nprint(rslt)\r\n<\/pre>\n
Enter some random number = 34879010\r\nThe sorted digits in ascending order of a given number 34879010 after removal of leading zeros =\r\n134789<\/pre>\n
\n