1. 程式人生 > >第4章:介紹python物件型別/4.1 python的核心資料型別/4.2 字串/4.2.4 字

第4章:介紹python物件型別/4.1 python的核心資料型別/4.2 字串/4.2.4 字

  • 字串格式化

  1. %s 方式格式化:

    >>> "%s,abc,%s" %('123','456')

    '123,abc,456'

  2. {數字}方式格式化:

    >>> "{0},abc,{1}".format('123','456')

    '123,abc,456'

  • 字串編輯HTML或者XML語法

  1. 語法:

    1. 以三個"""或者'''開始,結束也是對應的"""或者'''的,中間的可以是任意字元,包括"或者'

    2. 如果換行會自動補上\n

  2. 舉例:

    >>> msg = """cbd 123""

             

    xxxd ddd ''

              8997 xyz """

    >>> msg

    'cbd 123""\n          xxxd ddd \'\'\n          8997 xyz '  \n自動加上換行符


  1. 需要引入模組:import re

  2. 主匹配括號內匹配的內容

  3. 使用舉例:

    >>> match = re.match("hello(.*)","hello python world")

    >>> match.groups()

    (' python world',)   #括號內匹配的內容是python world'

    >>> match = re.match("/(.*)/(.*)/(.*)","/abc/cdr/xyz")

    >>> match.groups()

    ('abc', 'cdr', 'xyz')  #括號內匹配的內容是'abc', 'cdr', 'xyz'

    >>>


* 打折課程大放送
1. 【圖解+筆記】Python從入門到進階大合集詳細講解(含500條筆記):http://edu.51cto.com/sd/99cf7
2. 【筆記式】Python基礎入門--八大資料結構篇(含200條筆記):http://edu.51cto.com/sd/f967d
3. 【筆記式】Python基礎入門--語句函式篇(含150條筆記):http://edu.51cto.com/sd/eea85
4. 【圖解+筆記】Python進階-模組、包、面向物件篇(含200條筆記):http://edu.51cto.com/sd/43049
5. 【筆記式】Maven從入門到進階大合集詳細講解(含200條筆記):http://edu.51cto.com/sd/09299
6. 【筆記式】Maven基礎入門--座標倉庫私服(含100條筆記):http://edu.51cto.com/sd/e1387
7. 【筆記式】Maven高階進階--外掛測試屬性配置(含100條筆記):http://edu.51cto.com/sd/f3fda