什么叫字符串?字符串就是一个个字符组成的有序的序列,是字符的集合,在python中通常使用单引号、双引号和三引号引住的字符序列
区别于列表类型的对象,列表对象时可变的,而字符串对象是不可变的,因此字符串对象不存在通过索引改变其中的字符
字符串可以通过下标索引进行访问
字符串在内存中可以是连续的地址空间,有序的序列
举例说明:定义变量sql等于一个字符串,然后进行对字符串进行迭代
sql = """select * form user where name='tom'"""
for i in sql:
print(i)
‘string’.join(iterable) –>str
Return a string which is the concatenation of the strings in the iterable iterable.
1.将可迭代对象连接起来,使用string作为分隔符
2.可迭代对象本身元素都是字符串
############可迭代对象都是字符串#################
3.返回一个新的字符串
代码如下:
#!/bin/python3
#-*- coding: UTF-8 -*-
lst = ['1','2','3']
'#'.join(lst)
运行结果:
'1#2#3'
反规则代码:
#!/bin/python3
#-*- coding: UTF-8 -*-
lst = ['1',['a','b'],'3']
'#'.join(lst)
运行结果:
TypeError: sequence item 1: expected str instance, list found
将2个字符串穿了在一起
返回一个新的字符串
字符串统计通常使用len()函数
枚举函数本身:
def enumerate(collection):
'Generates an indexed series: (0,coll[0]), (1,coll[1]) ...'
i = 0
it = iter(collection)
while 1:
yield (i, it.next())
i += 1
代码示范:
#!/bin/python3
#-*- coding: UTF-8 -*-
s = "I'am"
print(len(s))
for i,item in enumerate(s):
print(i,item)
运行结果:
4
0 I
1 '
2 a
3 m
分隔字符串的方法分为两类
split系:将字符串按照分隔符分隔成若干字符串,并返回列表
partition:将字符串按照分隔符分隔成2段,返回这2段和分隔符的元组
split(sep=None,maxsplit=-1) sep表示分隔符 maxsplit表示最大分隔次数
1.Return a list of the words in the string, using sep asthe delimiter string
返回一个以sep分隔的列表
2.If sep is given,consecutive delimiters are not grouped together and are deemed to delimit empty strings
如果sep给定,对于连续的分隔符当做一个分隔符的字符串处理,相当于对分隔符转义
3.If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator
如果sep没有给定或者none,用正则表达式描述的话为’ ” “+ ‘即空白+,一个空白或者至少一个空白为分隔符进行处理
代码1
#!/bin/python3
#-*- coding: UTF-8 -*-
s1 = "I'm \ta super student."
print(s1.split())
运行结果:
["I'm", 'a', 'super', 'student.']
代码2
#!/bin/python3
#-*- coding: UTF-8 -*-
s1 = "I'm \ta super student."
#匹配第三条规则
print(s1.split('s'))
#匹配第二天规则
#特别要注意此条信息,以空格为分隔符,遇到\t并不会当作4个空格
print(s1.split(' '))
运行结果:
["I'm \ta ", 'uper ', 'tudent.']
["I'm", '\ta', 'super', 'student.']
partition返回元组代码
代码:
#!/bin/python3
#-*- coding: UTF-8 -*-
s = "I'am Cisco python"
print(s.partition("C"))
运行结果:
[python@centos7 330]$./partition.py
("I'am ", 'C', 'isco python')
splitlines(keepends) 若keepends=True时,表示列表中保留分隔符,遇到分隔的项目有如下:\n | \r | \r\n | \v | \f | \x1c | \x1d | \x1e | \x85 | \xu2028 | \u2029
代码1:
#!/bin/python3
#-*- coding: UTF-8 -*-
string = 'ab c\n\nde fg\rk1\r\n'
#在以行为分隔符时,同样会有遇到两个连续的分隔符当一个分隔符的字符对待
print(string.splitlines())
print(string.splitlines(keepends=True))
str.endswith(suffix[,start,[,end]]) suffix[ˈsʌfɪks]词尾的意思,start 就是字符串的索引位置,可以指定区间内是否以某字符串结尾
Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for
返回True或者Fasle,后缀也可以通过一个元组来查找,即查找条件可以是元组
#!/bin/python3
#-*- coding: UTF-8 -*-
s = "I'am very very very sorry"
print(s.endswith('very'))
print(s.endswith('very',5))
print(s.endswith('very',5,9))
运行结果:
False
False
True
str.endswith(prefix[,start,[,end]]) prefix[ˈpri:fɪks]前缀的意思,start 就是字符串的索引位置,可以指定区间内是否以某字符串结尾
#!/bin/python3
#-*- coding: UTF-8 -*-
s = "I'am very very very sorry"
print(s.startswith('very'))
print(s.startswith('very',5))
print(s.startswith('very',5,9))
运行结果:
False
True
True
count(sub [,start[,end]]) –>int
Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.
在指定的区间[start, end),从左至右,统计子串sub出现的次数
代码:
#!/bin/python3
#-*- coding: UTF-8 -*-
s = "I'am very very very sorry"
print(s.count('very'))
print(s.count('very',5))
print(s.count('very',5,14))
运行结果:
3
3
2
str.strip([chars]) strip[strɪp] 剥离、跳跃、除去 [chars]字符集 默认删除字符串前后的空白
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed.
返回一个开头和结尾被移除后赋值的字符串序列,如果字符集参数给定的话则移除字符集内的任一字
代码1:
#!/bin/python3
#-*- coding: UTF-8 -*-
a = ' spacious '
print(a)
b = a.strip()
print(b)
运行结果:
spacious
spacious
代码2:
#!/bin/python3
#-*- coding: UTF-8 -*-
a = 'www.baidu.com'
print(a.strip('w.com'))
print(a.lstrip('w.com'))
print(a.rstrip('w.com'))
运行结果:
baidu
baidu.com
www.baidu
str.find(sub[,start[,end]])
Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.
返回最小子串的索引,如果没有找到返回-1
str.index(sub[,start[,end]])
Like find(), but raise ValueError when the substring is not found
#!/bin/python3
#-*- coding: UTF-8 -*-
s = 'I am very very very sorry'
print(s.find('very'))
print(s.index('very'))
print(s.find('very',50,100))
print(s.index('very',50,100))
运行结果:
5
5
-1
Traceback (most recent call last):
File "./find.py", line 8, in
print(s.index('very',50,100))
ValueError: substring not found
str.replace(old,new[,count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
返回所有替换过复制的字符串对象,如果count给定的话,只有第一次计数的子字符串被替换,就是替换count次
#!/bin/python3
#-*- coding: UTF-8 -*-
s = 'I am very very very sorry'
print(s.replace('very','good'))
print(s.replace('very','good',2))
运行结果:
I am good good good sorry
I am good good very sorry
title() ->str
标题的每个单词都大写
capitalize() -> str
首个单词大写
center(width[,fillchar]) ->str
width打印宽度
fillchar填充的字符
zfill(width) ->str
width打印宽度,居右,左边用0填充
ljust(width[,fillchar]) ->str左对齐
rjust(width[,fillchar]) ->str右对齐
upper()全部大写
lower()全部小写
swapcase()交互大小写
isalnum() ->是否是字母和数字组成
isalpha() ->是否是字母
isdecimal() ->是否只包含十进制数字
isdigit() ->是否全部数字
isdentifiler ->是否是字母和下划线开头的字符标识符
islower() ->是否都是小写
isupper() ->是否全部大写
isspace() ->是否只包含空白字符