# 序列的操作
'''
序列的索引与反向索引
python是由C编写,序列实际上是一个对象
是一个用数组存储的字符串,故可以用数组下标来索引数据
'''
S ='luozekun'
A ='lu'
print( S[0], S[1], S[2] )
# 序列的反向索引如下,分别为倒数第一个第二个第三个元素
print( S[-1], S[-2], S[-3])
# 序列slice索引即提取出字符串的一部分
'''
一般形式为S[i:j],i为字符串数组中第i+1个元素开始偏移j个位置但不包括偏移量为J的内容
j的默认值为分别为0和分片序列的len
'''
A = S[1:3]# 取S中从第二个字符到第三个字符但不包括第三个字符的子字符串
print(A)
A = S[:4]# 第一个开始到第三个但不包括第三个
A = S[1:]# 第二个字符开始到最后一个
S[1:-1]# 第二个到倒数第二个
print(A)
'''
字符串的合并与find,replace,split,upper的使用
以及字符串的格式化
'''
# 计算序列长度
lenth =len(S)
# 用一个序列代替另一个序列的指定元素
S = S.replace('uo', 'xyz' )
print( lenth, S )
# 序列的合并
S = S +'JKL'
print(S)
# 序列分割
line ='aaa,bbb,cccc,dddddd'
line = line.split(',')# 以逗号为基准分割实际上是把数组分割然后形成新的数组
# 大小写转换
S = S.upper()
line = line[1].upper()# warning当序列split后不能够直接用序列名进行大小写转换而应该使用索引调用方法
print(S,line)
# 查找返回 -1 or 偏移量
S ='ABCK'
S = S.lower()
C = S.find('k')# 此时C 为 1
print(C)
C = S.find('O')# 此时C 为 -1
print(C)
# 判断序列是否全为字母,返回false和true
S = S +'1'
D = S.isalpha()#D此处为false
print(D)
# 格式匹配
T ='%s, dogs, and %s'%('pig','PIG')# T = pig, dogs, and PIG
print(T)
# 查看可以使用那些内置函数与内置函数的功能
print(dir(T))# 查看内置函数
print(help(T.replace))# 查看inner function function
# ord查看ascii编码
print(ord('\n'))
# 模式匹配
import re
N ='pig \thongbifu dog'
M ='/pig/dog/monkey'
'''
搜索N中以pig开头中间有空格或者制表符以dog结尾的子序列
(.*)表示任意字符 group是一个数组对象,将(.*)匹配的字符串放入group中因为只有一个(.*)
因此只有一个group
'''
match = re.match('pig[ \t]*(.*)dog',N)
print(match.group(1))
#根据格式将pig,dog,monkey放入group中因为有三个模式匹配字符因此要用groups
match = re.match('/(.*)/(.*)/(.*)','/pig/dog/monkey')
L = match.groups()
print(L[1],L[2],L[0],L)
'''
##################################
author:Jack rose
Date:2018.10.20
Program: 序列
#总结#
在python中字符串类型即序列数据类型
实际上是一个数组对象,有方法有也有
属性,因此字符串对象可以调用各种方法
字符串是以数组的形式存储的,因此可
以通过下标索引数据。
##################################
'''
'''
代码结果:
D:\PythonProgram\venv\Scripts\python.exe D:/PythonProgram/PythonString.py
l u o
n u k
uo
uozekun
8 lxyzzekun
lxyzzekunJKL
LXYZZEKUNJKL BBB
3
-1
False
pig, dogs, and PIG
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Help on built-in function replace:
replace(old, new, count=-1, /) method of builtins.str instance
Return a copy with all occurrences of substring old replaced by new.
count
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
If the optional argument count is given, only the first count occurrences are
replaced.
None
10
hongbifu
dog monkey pig ('pig', 'dog', 'monkey')
Process finished with exit code 0
'''
网友评论