{"id":19043,"date":"2021-08-29T15:13:33","date_gmt":"2021-08-29T09:43:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=19043"},"modified":"2021-11-22T18:37:08","modified_gmt":"2021-11-22T13:07:08","slug":"python-program-to-check-duck-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-duck-number\/","title":{"rendered":"Python Program to Check Duck Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert Decimal Number to Binary String using Recursion<\/a> A Duck number is one that has a zero in between or at the end of a given number.<\/p>\n For Example:<\/p>\n Let Number = 2300<\/p>\n It is a Duck number because it has zeros to the end.<\/p>\n Number= 00054<\/p>\n It is not a Duck number because it has zeros at the beginning. The actual value of a number is 54 if those zeros are removed.<\/p>\n Given a number and the task is to check if the given number is a Duck Number or not.<\/p>\n lstrip() Method in Python:<\/strong><\/p>\n The lstrip() method returns a string copy with the leading characters removed (based on the string argument passed).<\/p>\n Based on the argument, lstrip() removes characters from the left (a string specifying the set of characters to be removed).<\/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 check if the given number is a Duck Number or not:<\/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 Convert Decimal Number to Binary String using Recursion Duck Number: A Duck number is one that has a zero in between or at the end of a given number. For Example: Let Number = 2300 It is a Duck number because it has zeros to …<\/p>\n
\nDuck Number:<\/strong><\/p>\nGiven Number = 320045<\/pre>\n
The above given number { 320045 } is a Duck number<\/pre>\n
Given Number = 1400<\/pre>\n
The above given number { 1400 } is a Duck number<\/pre>\n
Program to Check Duck Number in Python<\/h2>\n
\n
Method #1: Using lstrip() Method (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 320045\r\n# Convert the given number to a string using the str() function and\r\n# store it in another variable.\r\nstr_numb = str(gvn_numb)\r\n# Apply the lstrip() method with the argument passed as 0 on the above string number\r\n# and store it in another variable.\r\nlstrip_num = str_numb.lstrip(\"0\")\r\n# Check if 0 is present in the above lstrip number using the if conditional statement.\r\nif(\"0\" in lstrip_num):\r\n # If the statement is true, then print \"The given number is a Duck Number\".\r\n print(\"The above given number {\", gvn_numb, \"} is a Duck number\")\r\nelse:\r\n # If the statement is false, print \"The given number is Not a Duck Number\".\r\n print(\"The above given number {\", gvn_numb, \"} is Not a Duck number\")\r\n<\/pre>\n
The above given number { 320045 } is a Duck number<\/pre>\n
Method #2: Using lstrip() Method (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and\r\n# store it in a variable.\r\ngvn_numb = int(input(\"Enter some random Number = \"))\r\n# Convert the given number to a string using the str() function and\r\n# store it in another variable.\r\nstr_numb = str(gvn_numb)\r\n# Apply the lstrip() method with the argument passed as 0 on the above string number\r\n# and store it in another variable.\r\nlstrip_num = str_numb.lstrip(\"0\")\r\n# Check if 0 is present in the above lstrip number using the if conditional statement.\r\nif(\"0\" in lstrip_num):\r\n # If the statement is true, then print \"The given number is a Duck Number\".\r\n print(\"The above given number {\", gvn_numb, \"} is a Duck number\")\r\nelse:\r\n # If the statement is false, print \"The given number is Not a Duck Number\".\r\n print(\"The above given number {\", gvn_numb, \"} is Not a Duck number\")\r\n<\/pre>\n
Enter some random Number = 000897\r\nThe above given number { 897 } is Not a Duck number<\/pre>\n
\n