1. 程式人生 > >python 字串替換

python 字串替換

字串替換可以用內建的方法和正則表示式完成。
1用字串本身的replace方法:

a = 'hello word'
b = a.replace('word','python')
print b

2用正則表示式來完成替換:

import re
a = 'hello word'
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b

相關推薦

python字串替換的2種有效方法

1是用字串本身的方法。 2用正則來替換字串 下面用個例子來實驗下: a = 'hello word' 我把a字串裡的word替換為python 1用字串本身的replace方法 a.replace('word','python') 輸出的結果是hello python

python 字串替換

字串替換可以用內建的方法和正則表示式完成。1用字串本身的replace方法: a = 'hello word'b = a.replace('word','python')print b 2用正則表示式來完成替換: import rea = 'hello word'strinfo = re.compile

python字串替換

print('---replace實現多個子串替換----')strr='abaabbaccaaeeaa'print('原字串:\n ',format(strr))e = strr.replace('aa',str(0))print('替換後的字串:\n ',format(e

python字串替換方法和注意事項

方法有兩種: last_date = “1/2/3”   目標為"123" 之一:repalce date =last_date.replace('/','') 之二:re p = re.compile("/") date = p.sub('', last_date)

[python]字串替換format和re.sub

python字串的格式化函式format功能很強大,可以使用一個字典來替代多個格式化字串,可以用來實現類似模板的功能。(replace只能替換單個字串,不適用)s2="discriminator {vrf} local {_bfdlocal} remote {_bfdremo

python 字串替換字元

今天本來打算寫個程式,替換字串中固定的一個字元:將<全部替換成回車'\n' 於是,我寫成這樣 s='sdjj<ddd<denj,>' for x in s: if x=='<': x='\n' print(s) 然後輸出還是 'sdjj&l

[work] Python 字串操作(擷取/替換/查詢/分割)

Python 擷取字串使用 變數[頭下標:尾下標],就可以擷取相應的字串,其中下標是從0開始算起,可以是正數或負數,下標可以為空表示取到頭或尾。 # 例1:字串擷取 str = '12345678' print str[0:1] >> 1   # 輸出s

劍指offer(python): 第二題 字串 替換空格

題目描述 請實現一個函式,將一個字串中的每個空格替換成“%20”。例如,當字串為We Are Happy.則經過替換之後的字串為We%20Are%20Happy。   看到題目的第一反應是找到空格直接替換就行了。簡單粗暴。  python的replece方法一

Python字串string的查詢和替換

hello_str = "hello world" # 1. 判斷空白字元 space_str = " \t\n\r" print(space_str.isspace()) # 2. 判斷是否以指定字串開始 print(hello_str.start

Python 字串操作(擷取/替換/查詢/分割)

Python 擷取字串使用 變數[頭下標:尾下標],就可以擷取相應的字串,其中下標是從0開始算起,可以是正數或負數,下標可以為空表示取到頭或尾。 # 例1:字串擷取 str = '12345678' print str[0:1] >> 1 # 輸出str位置0開始到位

Python正則表示式如何進行字串替換

Python正則表示式在使用中會經常應用到字串替換的程式碼。有很多人都不知道如何解決這個問題,下面的程式碼就告訴你其實這個問題無比的簡單,希望你有所收穫。 1.替換所有匹配的子串用newstring替換subject中所有與正則表示式regex匹配的子串 result

Python字串操作之字串搜尋與替換

11、字串中的搜尋和替換 11.1 str.find():正序字串查詢函式 函式原型: str.find(substr [,pos_start [,pos_end ] ] ) 返回str中第一

python常用的幾種字串替換函式strip、replace、sub

#!/usr/bin/env python # coding:utf-8 import re ''' 功能:對常見的幾種字串處理函式進行測試使用學習 Author:沂水寒城 ''' def str_test(): str_list=['We are family!!!', '00 11 2

Python字串替換

1.用字典存對應的替換規則,用迴圈取鍵值對應: dict1 = {'A':'U','U':'A','C':'G','G':'C'} def mRNA_tRNA(s): list

python字串替換

字串替換可以用內建的方法和正則表示式完成。 1用字串本身的replace方法: a = 'hello word' b = a.replace('word','python') print b 例子: #encoding=utf-8 # Created by doub

Python字串的連線、替換、擷取,以及字串格式化輸出,引號

一、字串的連線 (1)以下這種方法效率比較低,一般剛開始學時有可能會犯這個錯誤: s = ['a','b','c','d'] content = ' ' for i in a: content = content + i #在迴圈連線字串時,每連線一次,就要重新開闢

001_018 Python 一次完成多個字串替換

程式碼如下:  #encoding=utf-8 print '中國' # 一次完成多個字串替換 #利用正則表示式re的sub方法 import re def multiple_replace(text,adict): rx = re.compile('|'.jo

python字串之查詢與替換

find str.find(substr [,pos_start [,pos_end ] ] ) 正序字串查詢函式 str.index(substr [, pos_start, [ pos_end ] ] ) 正序字串查詢函式 str.r

Python字串拼接、擷取及替換方法總結

python字串連線有幾種方法,我開始用的第一個方法效率是最低的,後來看了書以後就用了後面的 2種效率高的方法,希望對大家有幫助。 先介紹下效率比較低的方法:a = ['a','b','c','d'] content = '' for i in a: content =

python--檔案刪除、判斷目錄存在、字串替換

昨晚筆試了金山WPS的測試開發卷,做個筆記(但不是答案,只是我的想法),關於檔案和字串的處理正在寫入與完善至上一篇的部落格中,現在題目如下: 1.使用指令碼語言刪除E:\abc目錄下的所有檔案:   利用os.path.exists()判斷E:\abc目錄是否存在,如果存在利