在本文中,我们将学习一个 python 程序,从以字符串形式给出的数字中删除前导零。
假设我们取了一个字符串格式的数字。我们现在将使用下面给出的方法删除所有前导零(数字开头存在的零)。
以下是用于完成此任务的各种方法 -
以下是执行所需任务要遵循的算法/步骤。−
以下程序以字符串的形式返回,该字符串使用 for 循环和 remove() 函数从作为字符串传递的数字中删除所有前导零 −
# creating a function that removes the leading zeros # from a number passed as a string to the function def deleteLeadingZeros(inputString): # traversing through the length of the string for k in range(len(inputString)): # checking whether the current character of a string is not 0 if inputString[k] != '0': # getting the remaining string using slicing outputString= inputString[k::] # returning resultant string after removing leading 0s return outputString # returning 0 if there are no leading 0s found in the entire string return "0" # input number as a string inputString = "0002056" print("Given String is:", inputString) # calling the deleteLeadingZeros() function by passing the input string to it print("After Removing Leading Zeros:", deleteLeadingZeros(inputString)) # checking it for other strings having no leading 0s inputString = "256" print("Given String is:", inputString) # calling the deleteLeadingZeros() function by passing the input string to it print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))
在执行时,上述程序将生成以下输出 -
Given String is: 0002056 After Removing Leading Zeros: 2056 Given String is: 256 After Removing Leading Zeros: 256
以下是执行所需任务要遵循的算法/步骤。−
以下程序以字符串形式返回,该字符串使用正则表达式从作为字符串传递的数字中删除所有前导零 -
# importing re module import re # creating a function that removes the leading zeros # from a number passed as a string to the function def deleteLeadingZeros(inputString): # regex pattern for removing leading zeros from an input string regexPattern = "^0+(?!$)" # Replace the matched regex pattern with an empty string outputString = re.sub(regexPattern, "", inputString) # returning output string after removing leading 0s return outputString # input number as a string inputString = "0002056" print("Given String is:", inputString) # calling the deleteLeadingZeros() function by passing the input string to it print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))
在执行时,上述程序将生成以下输出 -
Given String is: 0002056 After Removing Leading Zeros: 2056
以下是执行所需任务要遵循的算法/步骤。−
以下程序返回为一个数字,该数字使用 int() 函数从作为字符串传递的数字中删除所有前导零 -
# creating a function that removes the leading zeros # from a number passed as a string to the function def deleteLeadingZeros(inputString): # converting the input string to an integer removes all the leading zeros result = int(inputString) # returning the resultant number after removing leading zeros return result # input number as a string inputString = "0002056" print("Given String is:", inputString) # calling the deleteLeadingZeros() function by passing the input string to it print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))
在执行时,上述程序将生成以下输出 -
Given String is: 0002056 After Removing Leading Zeros: 2056
在本文中,我们学习了如何使用三种不同的方法从作为字符串给出的数字中删除前导零。我们学习了如何使用切片来获取可迭代对象的子集,例如字符串、列表或元组。我们还学习了如何利用正则表达式模块用另一种模式替换(替换)一种模式。