Python程序設計-清華大學出版社-董付國第4章 字符串與正則表達式_第1頁
Python程序設計-清華大學出版社-董付國第4章 字符串與正則表達式_第2頁
Python程序設計-清華大學出版社-董付國第4章 字符串與正則表達式_第3頁
Python程序設計-清華大學出版社-董付國第4章 字符串與正則表達式_第4頁
Python程序設計-清華大學出版社-董付國第4章 字符串與正則表達式_第5頁
已閱讀5頁,還剩50頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、 s=apple,peach,banana,pear li=s.split(,) liapple, peach, banana, pear s.partition(,)(apple, , peach,banana,pear) s.rpartition(,)(apple,peach,banana, , pear) s.rpartition(banana)(apple,peach, banana, ,pear)s = 2014-10-31 t=s.split(-) print t2014, 10, 31 print map(int, t)2014, 10, 31對于split()和rsplit()

2、方法,如果不指定分隔符,則字符串中的任何空白符號(包括空格、換行符、制表符等等)都將被認為是分隔符,返回包含最終分割結果的列表。 s = hello world nn My name is Dong s.split()hello, world, My, name, is, Dong s = nnhello world nnn My name is Dong s.split()hello, world, My, name, is, Dong s = nnhellott world nnn My namet is Dong s.split()hello, world, My, name, is,

3、Dongsplit()和rsplit()方法還允許指定最大分割次數,例如: s = nnhellott world nnn My name is Dong s.split(None,1)hello, world nnn My name is Dong s.rsplit(None,1)nnhellott world nnn My name is, Dong s.split(None,2)hello, world, My name is Dong s.rsplit(None,2)nnhellott world nnn My name, is, Dong s.split(None,5)hello,

4、world, My, name, is, Dong s.split(None,6)hello, world, My, name, is, Dongnlower()、upper()、capitalize()、title()、swapcase()這幾個方法分別用來將字符串轉換為小寫、大寫字符串、將字符串首字母變?yōu)榇髮憽⒚總€單詞的首字母變?yōu)榇髮懸约按笮懟Q。 s=What is Your Name? s2=s.lower() s2what is your name? s.upper()WHAT IS YOUR NAME? s2.capitalize()What is your, name? s.

5、title()What Is Your Name? s.swapcase()wHAT IS yOUR nAME?n內置函數eval() eval(3+4)7 a = 3 b = 5 eval(a+b)8 import math eval(help(math.sqrt)Help on built-in function sqrt in module math:sqrt(.) sqrt(x) Return the square root of x. eval(math.sqrt(3)1.7320508075688772 eval(aa)Traceback (most recent call las

6、t): File , line 1, in eval(aa) File , line 1, in NameError: name aa is not defined返回指定寬度的新字符串,原字符串居中、左對齊或右對齊出現在新字符串中,如果指定寬度大于字符串長度,則使用指定的字符(默認為空格)進行填充。 Hello world!.center(20) Hello world! Hello world!.center(20,=)=Hello world!= Hello world!.ljust(20,=)Hello world!= Hello world!.rjust(20,=)=Hello wo

7、rld!則表達式是普通字符串,可以匹配自身npjcython可以匹配python、jython、cythonna-zA-Z0-9可以匹配一個任意大小寫字母或數字nabc可以一個匹配任意除a、b、c之外的字符npython|perl或p(ython|erl)都可以匹配python或perln子模式后面加上問號表示可選。r(http:/)?(www.)?只能匹配、、和nhttp只能匹配所有以http開頭的字符串n(pattern)*:允許模式重復0次或多次n(

8、pattern)+:允許模式重復1次或多次n(pattern)m,n import re text = alpha. beta.gamma delta re.split(. +,text)alpha, beta, gamma, delta re.split(. +,text,maxsplit=2)#分割2次alpha, beta, gamma delta re.split(. +,text,maxsplit=1)#分割1次alpha, beta.gamma delta pat = a-zA-Z+ re.findall(pat,text) #查找所有單詞alpha, beta, gamma, d

9、elta pat = name text = Dear name. re.sub(pat,Mr.Dong,text) #字符串替換Dear Mr.Dong. s = a s d re.sub(a|s|d,good,s) #字符串替換good good good re.escape() #字符串轉義n刪除字符串中重復的空格 import re s=aaa bb c d e fff re.split(s+,s)aaa, bb, c, d, e, fff, re.split(s+,s.strip()aaa, bb, c, d, e, fff .join(re.split(s+,s.strip()aaa bb c d e fff .join(re.split(s

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論