Python 从字符串中删除前导零
在本文中,我们将学习使用Python编写一个程序,从以字符串形式给出的数字中删除前导零。
假设我们已经将一个数字以字符串的形式输入。我们将使用下面给出的方法来删除所有的前导零(即数字开头处的零)。
使用的方法
以下是用于完成该任务的各种方法:
- 使用For循环和remove()函数
-
使用正则表达式
-
使用int()函数
方法1:使用For循环和remove()函数
步骤
以下是执行所需任务的算法/步骤:
- 创建一个函数 deleteLeadingZeros() ,它从作为字符串传递给函数的数字中删除前导零。
-
使用 for循环 和len()函数遍历字符串的长度。
-
len()函数 - 当对象是字符串时,len()函数返回对象中的项数。当对象是字符串时,len()函数返回字符串中的字符数。
-
使用if条件语句和!=运算符检查字符串中当前字符是否不为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
结论
在本文中,我们学习了如何使用三种不同的方法从以字符串形式给出的数字中去除前导零。我们学习了如何使用切片来获取可迭代对象(如字符串、列表或元组)的子集。我们还学习了如何利用 regex 模块来替换(替代)一个模式为另一个。