python 字符串

python 字符串

 

1、索引

>>> s = range(1,10)
>>> s
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> s[0]
1
>>> s[5]
6
>>> s[-1]
9
>>> s[-3]
7

2、切片

>>> s = range(1,10)
>>> s
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> s[1:3]
[2, 3]
>>> s[0:3]
[1, 2, 3]
>>> s[:3]
[1, 2, 3]

>>> s[-3:-1]
[7, 8]
>>> s[-3:]
[7, 8, 9]

>>> s[1:7:2]
[2, 4, 6]
>>> s[::2]
[1, 3, 5, 7, 9]

3、抑制字符串转义

问题:

>>> s = 'C:\test1\notstr\vaaa.txt'
>>> print s
C:    est1
otstraaa.txt

解决办法1:

>>> s = 'C:\\test1\\notstr\\vaaa.txt'
>>> print s
C:\test1\notstr\vaaa.txt

解决办法2:

>>> s = r'C:\test1\notstr\vaaa.txt'
>>> print s
C:\test1\notstr\vaaa.txt

 

posted @ 2015-07-20 00:12  蛇吞象  阅读(166)  评论(0编辑  收藏  举报