博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 补前导零_Python正则表达式| 程序从IP地址中删除前导零
阅读量:2529 次
发布时间:2019-05-11

本文共 1342 字,大约阅读时间需要 4 分钟。

python 补前导零

Given an IP address as input, write a Python program to remove leading zeros from it.

给定一个IP地址作为输入,编写一个Python程序以从中删除前导零。

Examples:

例子:

Input:  216.08.094.196    Output: 216.8.94.196    Input: 216.08.004.096    Output: 216.8.4.96

In this program, we are using sub() method of "re" module.

在此程序中,我们使用“ re”模块的 sub()方法

Syntax:

句法:

re.sub(pattern, repl, string, count=0, flags=0)

The sub() in the function stands for SubString, a certain regular expression pattern is searched in the given string(3rd parameter), and upon finding the substring pattern is replaced by repl(2nd parameter), count checks and maintains the number of times this occurs.

在功能子()代表子串,一个特定正则表达式模式中搜索给定的字符串( 第三参数)中,并在找到的子图案由REPL(第2参数)代替,计数检查和维护数这种情况经常发生。

Code

# Python program to Remove leading zeros from an IP address# import re module# re module provides support# for regular expressionsimport re# Make a regular expression for # finding leading zeros in ip addressregex = '\.[0]*'	# Define a function for Remove# leading zeros from an IP addressdef removeLeadingZeros(ip):    modified_ip = re.sub(regex, '.', ip)    print(modified_ip)# Main code if __name__ == '__main__' : 		# Enter ip address 	ip = "216.08.094.196"		# call function 	removeLeadingZeros(ip)	ip = "216.08.004.096"	removeLeadingZeros(ip)

Output

输出量

216.8.94.196216.8.4.96

翻译自:

python 补前导零

转载地址:http://hzxzd.baihongyu.com/

你可能感兴趣的文章
Python -- machine learning, neural network -- PyBrain 机器学习 神经网络
查看>>
一道dp题目
查看>>
mysql中间件研究(tddl atlas cobar sharding-jdbc)
查看>>
Cast-128 加密算法和 MyPassWord 的破解
查看>>
4.28下午 听力611
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
《机器学习实战》学习笔记第二章 —— K-近邻算法
查看>>
uni-app 引入本地iconfont的正确姿势以及阿里图标引入
查看>>
DSB
查看>>
Java中的阻塞队列
查看>>
前端软件sublime的一些常用快捷键
查看>>
openssl 升级
查看>>
2017.10.30 天晴 昨天十公里没减肥
查看>>
Git 打标签(分布式版本控制系统)
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>