- str = '':空字串
- str = "I'm Lovin' It":字串內有單引號之類的符號時使用
- str = """...""":文字區塊
- str = r'raw strings':
- str = u'unicode strings':
- len(str):字串長度
- for l in str:一次拿一個字母出來,存在變數l裡面
- pattern in str:測試pattern是否在這個字串裡面
- str1 + str2:連結兩個字串
- str * 7:重複出現字串七次
- str[i:j]:子字串,見後面的說明
切子字串的i, j參照下面的示意圖(參考Python文件上的說明):
-6 -5 -4 -3 -2 -1
0 1 2 3 4 5
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+[: 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1 :]
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
>>> word[-1] # last character
'n'
>>> word[-2] # second-last character
'o'
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'
以上操作字串的方法,搭配《內建的Python字串處理功能》就可以在不用regular expression的方式處理處理文本
_EOF_
沒有留言:
張貼留言