码迷,mamicode.com
首页 > 编程语言 > 详细

python 字符串

时间:2015-07-20 01:19:17      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

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 = rC:\test1\notstr\vaaa.txt
>>> print s
C:\test1\notstr\vaaa.txt

 

python 字符串

标签:

原文地址:http://www.cnblogs.com/shetunxiang/p/4660247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!