1.字符串的定义
字符串是以’'或"“或”"" “”"括起来的任意文本
例如:
a = 'hello'
b = "python"
c = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
"""
我们测试a,b,c的类型确实均为字符串并打印,代码如下:
a = 'hello'
b = "python"
c = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
"""
print(type(a))
print(type(b))
print(type(c))
print(a)
print(b)
print(c)
2.字符串常用的转义符号
\n:换行
\t:一个tab键
"
’
#打印guido’s
print('guido\'s')
print("guido's")
#打印"hello guido’s python"
print('"hello guido\'s python"')
print("\"hello guido's python\"")
#\n和\t
print('%s\n%s' %(a,b)) #换行
print('%s\t%s' %(a,b)) #一个tab
3.字符串的特性
-1索引
s = 'hello'
#索引:0,1,2,3,4 索引是从0开始的
print(s[0]) #打印第一个字符h
print(s[4]) #打印第五个字符o
print(s[-1]) # 拿出字符串的最后一个字符o
-2切片
s = 'hello'
#s[start:end:step] 从start开始,到end-1结束,步长为step(默认是1)
print(s) #打印全部
print(s[0:3]) #打印前三个字符,hel
print(s[0:4:2]) #从第一个开始到三个结束,步长为2,打印hl
print(s[:]) #显示所有字符
print(s[:3]) #显示前3个字符
print(s[::-1]) #字符串倒序输出
print(s[1:]) #除了第一个字符之外,其他的全部显示
-3.重复
print(s*10) #输出十次hello
-4.连接
print('hello '+'world') #输出helloworld
-5.成员操作符
print('he' in s) #he在hello中,输出true
print('aa' in s) #aa不在hello中,输出false
print('he' not in s) #he在hello中,输出ture
4.匹配字符串的开头和结尾
filename.endswith(’.log’) #以’,log’结尾
url3.startswith(‘http://’) #以’http://'开头
filename='hello.logggh'
if filename.endswith('.log'): #endswith(' ')匹配结尾以',log'结尾
print(filename)
else:
print('error file')
url1 = 'file:///mnt'
url2 = 'ftp://172.25.254.250/pub/'
url3 = 'http://172.25.254.250/index.html'
if url3.startswith('http://'): #startswith(' ')匹配开头以'http://'开头
print('爬取网页')
else:
print('不能爬取网页')
6.去掉字符串两边空格和居中
去掉空格:
.strip()去掉两边空格
.lstrip()去掉左边空格
.rstrip()去掉右边空格
注意strip:对\n和\t依然生效
.strip(‘h’)去掉两边的h
.lstrip(‘he’)去掉左边的he
.rstrip(‘he’)去掉右边的he
.center(,) 居中
.ljust(,) 左对齐
.rjust(,) 右对齐
In [1]: s = ' hello'
In [2]: s.strip() #去掉两边空格
Out[2]: 'hello'
In [3]: s = ' hello '
In [4]: s.strip() #去掉两边空格
Out[4]: 'hello'
In [5]: s.lstrip() #去掉左边空格
Out[5]: 'hello '
In [6]: s.rstrip() #去掉右边空格
Out[6]: ' hello'
In [7]: s = '\nhello '
In [8]: s.strip() #有\n也去掉两边空格
Out[8]: 'hello'
In [9]: s = '\thello '
In [10]: s.strip()
Out[10]: 'hello' #有\t也去掉两边空格
In [11]: s = 'helloh'
In [12]: s.strip('h') #去掉两边的h
Out[12]: 'ello'
In [13]: s.strip('he') #去掉两边的he,由于右边没有e,所以只去掉h
Out[13]: 'llo'
In [14]: s.lstrip('he') #去掉左边的he
Out[14]: 'lloh'
In [15]: s.rstrip('he') #去掉右边的he,由于右边没有e,所以只去掉h
Out[15]: 'hello'
In [17]: print('学生管理系统'.center(50,'*')) #以第25个字符为中心
**********************学生管理系统**********************
In [18]: print('学生管理系统'.ljust(50,'*')) #左对齐
学生管理系统********************************************
In [19]: print('学生管理系统'.rjust(50,'*')) #右对齐
********************************************学生管理系统
7.搜索、替换、统计
find:搜索
replace:替换
count:统计
In [20]: s = 'hello python,learn python'
In [21]: s.find('python')
Out[21]: 6
In [22]: s.rfind('python')
Out[22]: 19
In [23]: s.replace('python','linux')
Out[23]: 'hello linux,learn linux'
In [24]: s1 = s.replace('python','linux')
In [25]: s1
Out[25]: 'hello linux,learn linux'
In [26]: s
Out[26]: 'hello python,learn python'
In [26]: s
Out[26]: 'hello python,learn python'
In [27]: s.count('python')
Out[27]: 2
In [28]: s.count('p')
Out[28]: 2
In [29]: s.count('i')
Out[29]: 0
8.分离和拼接
split:分离
join:拼接
In [30]: ip = '172.25.254.10'
In [31]: ip1 = '1172.25.254.10'
In [32]: ip1.split('.') #以字符串中的.分离
Out[32]: ['1172', '25', '254', '10']
In [33]: date = '2018-11-18'
In [34]: date.split('-') #以字符串中的-分离
Out[34]: ['2018', '11', '18']
In [35]: date.split('.') #没有按照需要分离的符号时,不变
Out[35]: ['2018-11-18']
In [37]: date.replace('-','/') #-替换成/
Out[37]: '2018/11/18'
In [38]: ip = ['1172', '25', '254', '10']
In [39]: ''.join(ip) #以空拼接
Out[39]: '11722525410'
In [40]: ':'.join(ip) #以:拼接
Out[40]: '1172:25:254:10'
In [41]: '*'.join(ip) #以*拼接
Out[41]: '1172*25*254*10'