python宏替換函數(shù) python 宏

如何用Python來進(jìn)行查詢和替換一個文本字符串

1、說明

創(chuàng)新互聯(lián)是專業(yè)的浦江網(wǎng)站建設(shè)公司,浦江接單;提供成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行浦江網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

可以使用find或者index來查詢字符串,可以使用replace函數(shù)來替換字符串。

2、示例

1)查詢

'abcdefg'.find('cde')

結(jié)果為2

'abcdefg'.find('acde')

結(jié)果為-1

'abcdefg'.index('cde')

結(jié)果為2

2)替換

'abcdefg'.replace('abc','cde')

結(jié)果為'cdedefg'

3、函數(shù)說明

1)find(...)

S.find(sub[, start[, end]]) - int

返回S中找到substring sub的最低索引,使得sub包含在S [start:end]中。 可選的 參數(shù)start和end解釋為切片表示法。

失敗時返回-1。

2)index(...)

S.index(sub[, start[, end]]) - int

與find函數(shù)類似,但是當(dāng)未找到子字符串時引發(fā)ValueError。

3)replace(...)

S.replace(old, new[, count]) - str

返回S的所有出現(xiàn)的子串的副本舊換新。 如果可選參數(shù)計(jì)數(shù)為給定,只有第一個計(jì)數(shù)出現(xiàn)被替換。

python的replace函數(shù)怎么用

Python replace()方法把字符串中的old(舊字符串)替換成new(新字符串),如果指定三個參數(shù)max,則替換不超過max次。

語法

replace()方法語法:

str.replace(old, new[, max])

參數(shù)

old -- 將被替換的子字符串;

new -- 新字符串,用于替換old子字符串;

max -- 可選字符串,替換不超過max次。

返回值

返回字符串中的old(舊字符串)替換成new(新字符串)后生成的新字符串,如果指定第三個參數(shù)max,則替換不超過max次。

實(shí)例

#!/usr/bin/python

str = "this is string example....wow!!! this is really string";

print str.replace("is", "was");

print str.replace("is", "was", 3);

輸出結(jié)果

thwas was string example....wow!!! thwas was really string

thwas was string example....wow!!! thwas is really string

python如何實(shí)現(xiàn)類似foxpro中的宏替換功能

首先要說下,比較兩個是否相等,應(yīng)該用==才對。

關(guān)于運(yùn)行字符串,可以用eval。

a=10

c="a==10"

if eval(c):

print "a=10"

a=10

本文名稱:python宏替換函數(shù) python 宏
URL分享:http://muchs.cn/article20/doegijo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、域名注冊用戶體驗(yàn)、建站公司軟件開發(fā)、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)