99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

Python學(xué)習(xí)之String模塊詳解

發(fā)布時間:2017-08-23 14:35  回復(fù):0  查看:2525   最后回復(fù):2017-08-23 14:35  
本文和大家分享的主要是python String 模塊相關(guān)內(nèi)容,一起來看看吧,希望對大家 學(xué)習(xí)python有所幫助。
  String 模塊包含大量實用常量和類,以及一些過時的遺留功能,并還可用作字符串操作。
   1. 常用方法
  str.capitalize() 把字符串的首字母大寫
  str.center(width) 將原字符串用空格填充成一個長度為 width 的字符串,原字符串內(nèi)容居中
  str.count(s) 返回字符串 s str 中出現(xiàn)的次數(shù)
  str.decode(encoding=’UTF-8’,errors=’strict’) 以指定編碼格式解碼字符串
  str.encode(encoding=’UTF-8’,errors=’strict’) 以指定編碼格式編碼字符串
  str.endswith(s) 判斷字符串 str 是否以字符串 s 結(jié)尾
  str.find(s) 返回字符串 s 在字符串 str 中的位置索引,沒有則返回 -1
  str.index(s) find() 方法一樣,但是如果 s 不存在于 str 中則會拋出異常
  str.isalnum() 如果 str 至少有一個字符并且都是字母或數(shù)字則返回 True, 否則返回 False
  str.isalpha() 如果 str 至少有一個字符并且都是字母則返回 True, 否則返回 False
  str.isdigit() 如果 str 只包含數(shù)字則返回  True  否則返回  False
  str.islower() 如果 str 存在區(qū)分大小寫的字符,并且都是小寫則返回 True  否則返回 False
  str.isspace() 如果 str 中只包含空格,則返回  True ,否則返回  False
  str.istitle() 如果 str 是標(biāo)題化的 ( 單詞首字母大寫 ) 則返回 True ,否則返回 False
  str.isupper() 如果 str 存在區(qū)分大小寫的字符,并且都是大寫則返回 True  否則返回 False
  str.ljust(width) 返回一個原字符串左對齊的并使用空格填充至長度 width 的新字符串
  str.lower() 轉(zhuǎn)換 str 中所有大寫字符為小寫
  str.lstrip() 去掉 str 左邊的不可見字符
  str.partition(s) s str 切分成三個值
  str.replace(a, b) 將字符串 str 中的 a 替換成 b
  str.rfind(s) 類似于  find() 函數(shù),不過是從右邊開始查找
  str.rindex(s) 類似于  index() ,不過是從右邊開始
  str.rjust(width) 返回一個原字符串右對齊的并使用空格填充至長度 width 的新字符串
  str.rpartition(s) 類似于  partition() 函數(shù) , 不過是從右邊開始查找
  str.rstrip() 去掉 str 右邊的不可見字符
  str.split(s) s 為分隔符切片 str
  str.splitlines() 按照行分隔,返回一個包含各行作為元素的列表
  str.startswith(s) 檢查字符串 str 是否是以 s 開頭,是則返回 True ,否則返回 False
  str.strip() 等于同時執(zhí)行 rstrip() lstrip()
  str.title() 返回 標(biāo)題化 str, 所有單詞都是以大寫開始,其余字母均為小寫
  str.upper() 返回 str 所有字符為大寫的字符串
  str.zfill(width) 返回長度為  width  的字符串,原字符串 str 右對齊,前面填充 0
   2.字符串常量
  string.ascii_lowercase 小寫字母 ’abcdefghijklmnopqrstuvwxyz’
  string.ascii_uppercase 大寫的字母 ’ABCDEFGHIJKLMNOPQRSTUVWXYZ’
  string.ascii_lettersascii_lowercase ascii_uppercase 常量的連接串
  string.digits 數(shù)字 0 9 的字符串 :’0123456789’
  string.hexdigits 字符串 ’0123456789abcdefABCDEF’
  string.letters 字符串 ’abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’
  string.lowercase 小寫字母的字符串 ’abcdefghijklmnopqrstuvwxyz’
  string.octdigits 字符串 ’01234567’
  string.punctuation 所有標(biāo)點字符
  string.printable 可打印的字符的字符串。包含數(shù)字、字母、標(biāo)點符號和空格
  string.uppercase 大學(xué)字母的字符串 ’ABCDEFGHIJKLMNOPQRSTUVWXYZ’
  string.whitespace 空白字符  ‘\t\n\x0b\x0c\r ‘
   3.字符串模板Template
  通過string.Template 可以為 Python 定制字符串的替換標(biāo)準(zhǔn) , 下面是具體列子:
  >>>from string import Template>>>s = Template('$who like $what')>>>print s.substitute(who='i', what='python')
  i like python
  >>>print s.safe_substitute(who='i') #  缺少 key 時不會拋錯
  i like $what
  >>>Template('${who}LikePython').substitute(who='I') #  在字符串內(nèi)時使用 {}'ILikePython'
  Template 還有更加高級的用法,可以通過繼承 string.Template,  重寫變量 delimiter( 定界符 ) idpattern( 替換格式 ),  定制不同形式的模板。
   import string
  template_text = ''' Delimiter : $de Replaced : %with_underscore Ingored : %notunderscored '''
  d = {'de': 'not replaced',
  'with_underscore': 'replaced',
  'notunderscored': 'not replaced'}
   class  MyTemplate(string.Template):
  重寫模板 定界符 (delimiter) "%",  替換模式 (idpattern) 必須包含下劃線 (_)
  delimiter = '%'
  idpattern = '[a-z]+_[a-z]+'
   print string.Template(template_text).safe_substitute(d)  #  采用原來的 Template 渲染
   print MyTemplate(template_text).safe_substitute(d)  #  使用重寫后的 MyTemplate 渲染
  輸出:
  Delimiter : not replaced
  Replaced : %with_underscore
  Ingored : %notunderscored
  Delimiter : $de
  Replaced : replaced
  Ingored : %notunderscored
  原生的Template 只會渲染界定符為 $ 的情況,重寫后的 MyTemplate 會渲染界定符為 % 且替換格式帶有下劃線的情況。
   4.常用字符串技巧
  · 1. 反轉(zhuǎn)字符串
>>> s = '1234567890'
>>> print s[::-1]
  0987654321
  · 2. 關(guān)于字符串鏈接
  盡量使用join() 鏈接字符串,因為 ’+’ 號連接 n 個字符串需要申請 n-1 次內(nèi)存 , 使用 join() 需要申請 1 次內(nèi)存。
  · 3. 固定長度分割字符串
>>>  import re
>>> s = '1234567890'
>>> re.findall(r'.{1,3}', s)  #  已三個長度分割字符串
  ['123', '456', '789', '0']
  ·4. 使用 () 括號生成字符串
  sql = (' SELECT  count()  FROM  table '
  ' WHERE  id = "10" '
  ' GROUP  BY sex')
  print sql
  SELECT count() FROM table WHERE id = "10" GROUP BY sex
  · 5. print 的字符串寫到文件
  >>> print >> open("somefile.txt", "w+"), "Hello World"  # Hello World 將寫入文件 somefile.tx
來源: 伯樂在線


您還未登錄,請先登錄

熱門帖子

最新帖子

?