字符串是Python的有序集合,序列中包含了一个从左到右的顺序——序列中的元素根据它们的相对位置进行存储和读取。
字符串的操作:
>>>S = ‘Spam‘ >>>S[0] #读取第0个字符 ‘S’ >>>len(S) #字符串的长度 4 >>>S[4] 读取第4个字符,越界访问报错 IndexError: string index out of range >>>S[1:3] ‘pa‘
反向访问:
>>>S[-1] ‘m‘ >>>S[:-1] ‘Spa‘ >>>S[:] ‘Spam‘ >>>print S[-3:-1] ‘pa‘
作为一个序列,字符串也支持使用家好进行合并:
>>>S = ‘Spam‘ >>>print(S+‘xyz‘) ‘Spamxyz‘ >>>S*4 ‘SpamSpamSpamSpam‘ >>>print [S]*4 [‘Spam‘, ‘Spam‘, ‘Spam‘, ‘Spam‘]
不可变性:
>>>S[0] = ‘s‘ TypeError: ‘str‘ object does not support item assignment >>>S = ‘z‘ + S[1:] ‘zpam‘
字符串的特定方法:
#查找字符串出现首次的位置,若没找到,返回-1 >>>S.find(‘pa‘) 1 >>>S = ‘Spampa‘ #replace全局搜索并替代,返回新的字符串 >>>print S.replace(‘pa‘, ‘xyz‘) ‘Sxyzmxyz‘ #replace不会修改原始的字符串 >>>S ‘Spampa‘ #分隔符将字符串拆分成子串 >>>line = ‘aaa,bbb,ccc,ddd‘ >>>line.split(‘,‘) [‘aaa‘, ‘bbb‘, ‘ccc‘, ‘ddd‘] #大小写变换 >>>line.upper() ‘AAA,BBB,CCC,DDD‘ #判断是否全是字母 >>>line.isalpha() False #去除行末的字符 >>>line = ‘aaa,bb b,ccc ,ddd\n‘ >>>line.rstrip() ‘aaa,bb b,ccc ,ddd‘ >>>line = ‘8888aaa,bbbb,888888‘ >>>line.rstrip(‘8‘) 8888aaa,bbbb,
字符串的格式化高级替代操作,可以以一个表达式的形式和一个字符串方法调用形式使用:
>>>print(‘%s,eggs,and %s%d‘%(‘spam‘,‘SPAM‘,10)) spam,eggs,and SPAM >>>print(‘{0}, eggs, and {1}‘.format(‘spam‘, ‘SPAM!‘)) spam, eggs, and SPAM!
寻求帮助dir()和help()
>>>dir(S) [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__getslice__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘_formatter_field_name_split‘, ‘_formatter_parser‘, ‘capitalize‘, ‘center‘, ‘count‘, ‘decode‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdigit‘, ‘islower‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]
dir函数简单的给出了方法的名称。要查询具体方法的具体用法,可以将方法传递给help():
>>>help(s.replace) replace(...) S.replace(old, new[, count]) -> string Return a copy of string S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
编写字符串的其他方法:
三个引号(单引号 或者 双引号):将所有行都合并在一起,并在每一行的末尾增加换行符。
>>>msg = """aaaaaaaaaaa bbb‘‘‘5555555555""88888888‘2222 99999999999999""" >>>msg aaaaaaaaaaa bbb‘‘‘5555555555""88888888‘2222 99999999999999
Python也支持原始的字符串常量(raw),即去掉反斜线的转义机制:
>>>msg = "abcd\nhhhhhh" abcd hhhhhh >>>msg = r"abcd\nhhhhhh" abcd\nhhhhhh
Python还支持Unicode字符串形式从而支持国际化。
模式匹配:
需要导入re模块。这个模块包含了类似搜索、分割和替换等调用。
>>>import re >>>match = re.match(‘hello[\t]*(.*)world‘, ‘hello Python world‘) >>>match.group(1) ‘ Python‘
时间: 2024-11-08 22:33:12