使用 Python 从作为字符串给出的数字中删除前导零

在本文中,我们将学习一个 python 程序,从以字符串形式给出的数字中删除前导零。

假设我们取了一个字符串格式的数字。我们现在将使用下面给出的方法删除所有前导零(数字开头存在的零)。

使用的方法

以下是用于完成此任务的各种方法 -

  • 使用 For 循环和 remove() 函数

  • 使用正则表达式

  • 使用 int() 函数

方法 1:使用 For 循环和 remove() 函数

算法(步骤)

以下是执行所需任务要遵循的算法/步骤。−

  • 创建一个函数 deleteLeadingZeros(),该函数从作为字符串传递给函数的数字中删除前导零。

  • 使用 for 循环,使用 len() 函数遍历字符串的长度。

  • len() 函数 − 对象中的项数由 len() 方法返回。当对象是字符串时,len() 函数返回字符串中的字符数。

  • 使用 if 条件语句和 != 运算符检查字符串中的当前字符是否不为 0

  • 使用切片获取前导零之后的字符串的剩余字符。

  • 从输入字符串中删除所有前导 0 后返回结果字符串。

  • 如果未找到前导 0,则返回 0。

  • 创建一个变量来存储作为字符串传递的输入数字。

  • 调用上面定义的 deleteLeadingZeros() 函数,方法是将输入字符串传递给它,以便在删除前导零后获取结果字符串。

  • 以同样的方式检查没有前导零的其他字符串。

以下程序以字符串的形式返回,该字符串使用 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

方法 2:使用正则表达式

算法(步骤)

以下是执行所需任务要遵循的算法/步骤。−

  • 使用 import 关键字导入正则表达式(re) 模块。

  • 创建一个函数 deleteLeadingZeros(),该函数从作为字符串传递给函数的数字中删除前导零。

  • 创建一个变量来存储用于从输入字符串中删除前导零的正则表达式模式。

  • 使用 sub() 函数将匹配的正则表达式模式替换为空字符串。

  • sub() 函数(返回一个字符串,其中给定模式的所有匹配匹配项都替换为替换字符串)。

  • 从输入字符串中删除所有前导 0 后打印生成的字符串。

以下程序以字符串形式返回,该字符串使用正则表达式从作为字符串传递的数字中删除所有前导零 -

# 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

方法 3:使用 int() 函数

算法(步骤)

以下是执行所需任务要遵循的算法/步骤。−

  • 创建一个函数 deleteLeadingZeros(),该函数从作为字符串传递给函数的数字中删除前导零。

  • 使用 int() 函数(从给定对象返回一个整数)将输入字符串转换为整数。此函数删除所有前导零。

  • 从输入字符串中删除所有前导 0 后返回结果数字。

以下程序返回为一个数字,该数字使用 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

结论

在本文中,我们学习了如何使用三种不同的方法从作为字符串给出的数字中删除前导零。我们学习了如何使用切片来获取可迭代对象的子集,例如字符串、列表或元组。我们还学习了如何利用正则表达式模块用另一种模式替换(替换)一种模式。

猜你喜欢

转载自blog.csdn.net/linyichao123/article/details/129171976