Python-字符串

  • 字符串不可變

引號

單雙引號都同樣:python

s = "hello"
s = 'hello'

單雙引號互相嵌套git

s = 'hello "world" !'
s = "hello 'world' !"

多行文本(經常使用於包含像HTML或XML這樣的內容):api

>>> s = """aaaaa
... bbbbb'a'a'a
... ccccc"""
>>> s
"aaaaa\nbbbbb'a'a'a\nccccc"

長度和下標訪問

>>> s = "hello"
>>> len(s)
5
>>> s[0]
'h'
>>> s[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

不適用於中文字符串:ssh

>>> s = "你好"
>>> s[0]
'\xe4'
>>> s[1]
'\xbd'
>>> len(s)
6

反向索引

>>> s = "hello"
>>> s[-1]
'o'
>>> s[-2]
'l'
>>> s[len(s)-1]
'o'

分片

>>> s = "abcdefg"
>>> s[1:3]
'bc'

包括左邊界1但不包括右邊界3.ui

默認值:左邊界默認爲0,右邊界默認爲字符串長度。spa

還能夠有第三個參數,表示步進,步進的默認值爲1:code

>>> s = 'abcdefg'
>>> s[1:len(s):2]
'bdf'

步進還能夠是負值:orm

>>> s[len(s):0:-2]
'gec'

字符串鏈接

>>> s1 = "abc"
>>> s2 = "def"
>>> s1 + s2
'abcdef'
>>> s1 * 3
'abcabcabc'
>>> title = "Meaning " 'of' " Life"
>>> title
'Meaning of Life'

字符串查找

s.find("")  # 返回下標,若是找不到,返回-1
s.index("")  # 返回下標,若是找不到,報錯

不轉義

使用原始(raw)字符串常量,去掉反斜線轉義機制。對象

>>> path = r'C:\new\text.data'
>>> path
'C:\\new\\text.data'

轉換

>>> int("42"), str(42)
(42, '42')
>>> float("1.5"), str(3.14)
(1.5, '3.14')

ASCII碼轉換:索引

>>> ord('s')
115
>>> chr(115)
's'

格式化

>>> 'That is %d %s bird!' % (1, 'dead')
'That is 1 dead bird!'

%[(name)][flags][width][.precision]typecode

typecode取值以下:

代碼 意義
s 字符串或任意對象
r s, 但使用repr而不是str
c 字符
d 十進制整數
i 整數
u 無符號(整數)
o 八進制整數
x 十六進制整數
X x,但打印大寫
e 浮點指數
E e,大寫
f 浮點數
F 浮點數
g e或f
G E或f
% 常量%

flags取值:

  • 左對齊 '%-6d' % 1234 => '1234 '
  • 正負號 '%+6d' % 1234 => ' +1234'
  • 0 補零 '%06d' % 1234 => '001234'

浮點數:

>>> x = 1.23
>>> '%-6.2f | %05.2f | %+06.1f' % (x, x, x)
'1.23   | 01.23 | +001.2'

使用 * 指定從參數列表中獲取width和precision:

>>> '%f, %.2f, %.*f' % (1/3.0, 1/3.0, 4, 1/3.0)
'0.333333, 0.33, 0.3333'

使用字典填充:

>>> '%(n)d %(x)s' % {"n":1, "x":"spam"}
'1 spam'

字符串help

>>> dir(s)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

具體某個屬性的做用:

>>> help(s.find)

Help on built-in function find:

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

    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

模式匹配

import re

match = re.match("Hello[ \t]*(.*)world", "Hello python world")

print(match.groups())  # ('python ',)

print(match.group(1))  # python 0是整個字符串
相關文章
相關標籤/搜索