日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python 补前导零_Python正则表达式| 程序从IP地址中删除前导零

發(fā)布時(shí)間:2025/3/11 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 补前导零_Python正则表达式| 程序从IP地址中删除前导零 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python 補(bǔ)前導(dǎo)零

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

給定一個(gè)IP地址作為輸入,編寫(xiě)一個(gè)Python程序以從中刪除前導(dǎo)零。

Examples:

例子:

Input: 216.08.094.196Output: 216.8.94.196Input: 216.08.004.096Output: 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.

在功能子()代表子串,一個(gè)特定正則表達(dá)式模式中搜索給定的字符串( 第三參數(shù))中,并在找到的子圖案由REPL(第2參數(shù))代替,計(jì)數(shù)檢查和維護(hù)數(shù)這種情況經(jīng)常發(fā)生。

Code

# Python program to Remove leading zeros from an IP address# import re module# re module provides support # for regular expressions import re# Make a regular expression for # finding leading zeros in ip address regex = '\.[0]*'# Define a function for Remove # leading zeros from an IP address def 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.196 216.8.4.96

翻譯自: https://www.includehelp.com/python/python-regex-program-to-remove-leading-zeros-from-an-ip-address.aspx

python 補(bǔ)前導(dǎo)零

總結(jié)

以上是生活随笔為你收集整理的python 补前导零_Python正则表达式| 程序从IP地址中删除前导零的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。