Python字串

字串字面值

    1. "abc"              #  ascii in python 2.7.x, unicode python 3.x
    2. u"中文"             # unicode 編碼
    3. b"abc"             #  ascii in python 2.7.x, bytes in python 3.x
    4. u"\u1234"          #  unicode 編碼

內建字串運算

s = “123”

    1. s[0]   == "1"         # 索引運算
    2. s[-1]  == "3"         # 負索引
    3. s[1:3] == "23"        # 切片
    4. s[0:3] == "123"       # 切片
    5. s[:3]  == s[0:3]      # True
    6. s[:-1] == s[0:2]      # True
    7. s[:]   == "abc"       # 拷貝
    8. s + "456" == "123456" # 串接
    9. s * 3 == "123123123"  # 重複

字串不可變性

python的字串不可變
也就是不存在這種用法 s[0] = “a”
使用串接替代 s = “a” + s[1:]