当前位置:首页 > 编程技术 > 正文内容

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

yc8881年前 (2023-02-22)编程技术436

使用 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

结论

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


本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!


从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!


本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。


本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。


若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。


本文链接:https://www.10zhan.com/biancheng/10587.html

标签: Python
分享给朋友:

“使用 Python 从作为字符串给出的数字中删除前导零” 的相关文章

【说站】laravel实现自定义404页面并给页面传值

【说站】laravel实现自定义404页面并给页面传值

以 laravel5.8 为例,虽然有自带的404页面,但太简单,我们更希望能自定义404页面,将用户留在站点。实现的方式很简单,将自定义的视图文件命名为 404.blade.php,并放到 reso...

【说站】Thymeleaf报错Error resolving template “XXX”

【说站】Thymeleaf报错Error resolving template “XXX”

修改了一下开源项目的目录结构访问突然报错Error resolving template “XXX”可能原因有如下三种:第一种可能:原因:在使用springboot的过程中,如果使用thymeleaf...

【说站】用一句话就可以去除宝塔面板操作上的二次验证

【说站】用一句话就可以去除宝塔面板操作上的二次验证

用过宝塔的朋友应该都会发现,现在宝塔面板有些鸡肋的功能,删除文件、删除数据库、删除站点等操作都需要做计算题!不仅加了几秒的延时等待,还无法跳过!这时候就会有朋友在想,如何去除宝塔面板的二次验证,此篇文...

【说站】利用Webhook实现Java项目自动化部署

【说站】利用Webhook实现Java项目自动化部署

用webhook就能实现Java项目自动部署,其实原理很简单。费话不多说,直接往下看教程。1. 创建gitee仓库并初始化2. 在linux安装git3. 在宝塔的软件的商店里下载Webhook4....

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

今天在安装MySQL时出现starting the server失败,经过查询分析得出以下结论,记录一下操作步骤。原因分析:如果电脑是第一次安装MySQL,一般不会出现这样的报错。如下图所示。star...

【说站】vagrant实现linux虚拟机的安装并配置网络

【说站】vagrant实现linux虚拟机的安装并配置网络

一、VirtualBox的下载和安装1、下载VirtualBox官网下载:https://www.virtualbox.org/wiki/Downloads我的电脑是Windows的,所以下载Wind...