首页 > 其他 > 详细

《转》序列字符串

时间:2019-10-31 17:46:41      阅读:81      评论:0      收藏:0      [点我收藏+]

 http://www.cnblogs.com/BeginMan/archive/2013/06/08/3125502.html

二、序列类型

包含字符串、列表、元祖。模式都一样,举一反三即可。如:

1、成员关系操作符(in / not in )

2、关于切片

1
2
3
4
5
6
s=[1,2,3,4]
print s[::-1]     #下标范围[0,0],步长是-1,则从后(4,包括4)往前切取所有,输出:[4, 3, 2, 1]
print s[::-2]     #下标范围[0,0],步长是-2,则从后(4,包括4)往前跳过2位切取,输出:[4, 2]
print s[::]        #下标范围[0,0],步长是0,则从前(1,包括1)往后切取所有,输出:[1, 2, 3, 4]
print s[::2]       #下标范围[0,0],步长是2,则从前(1,包括1)往后跳过2位切取,输出:[1, 3]
print s[1:4:2]  #下标范围[1,4],步长是2,则从下标为1(2)到下标为4(4)跳过2位切取,输出:[2, 4]

 要灵活运用。

三、关于序列类型的内建函数

如list()、tuple()、str()类型转换,实际上是工厂函数,浅copy的结果而并非真正的改头换面(转换)。

注意在string类型上应用list()、tuple()往往并不能得到我们想要的结果。

序列类型的内建函数一览表:

 cmp()、len()、max()、min()、enumerate()、zip()、

四、Unicode字符串

1 >>> ‘hello‘+u‘ ‘+‘world‘
2 u‘hello world‘

五、字符串类型的内建方法

1、不常用的string模块

技术分享图片
 1 >>> import string
 2 >>> string.uppercase
 3 ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘
 4 >>> string.lowercase
 5 ‘abcdefghijklmnopqrstuvwxyz‘
 6 >>> string.whitespace
 7 ‘\t\n\x0b\x0c\r ‘
 8 >>> string.digits
 9 ‘0123456789‘
10 >>> string.punctuation
11 ‘!"#$%&\‘()*+,-./:;<=>?@[\\]^_`{|}~‘
技术分享图片

打开这个string.py模块,如下:

技术分享图片
.......................................
# Some strings for ctype-style character classification
whitespace = ‘ \t\n\r\v\f‘
lowercase = ‘abcdefghijklmnopqrstuvwxyz‘
uppercase = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘
letters = lowercase + uppercase
ascii_lowercase = lowercase
ascii_uppercase = uppercase
ascii_letters = ascii_lowercase + ascii_uppercase
digits = ‘0123456789‘
hexdigits = digits + ‘abcdef‘ + ‘ABCDEF‘
octdigits = ‘01234567‘
punctuation = """!"#$%&‘()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + letters + punctuation + whitespace

# Case conversion helpers
.......................................
技术分享图片

不常用,很多功能可以自己模拟。

2、内建函数
技术分享图片

技术分享图片

技术分享图片

《转》序列字符串

原文:https://www.cnblogs.com/cmybky/p/11772232.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!