一.字符串的定义
a = "hello"
b = 'westos'
c = "what's up"
d = 'what\'s up'
print(a)
print(b)
print(c)
print(d)
测试结果:
hello
westos
what's up
what's up
注意:字符串中的单引号需要转义
二.字符串的特性:
索引:
<python中字符串中的字符是通过索引来提取的,索引从0开始,
同样也可从末尾提取,最后一个字符为-1>
s = 'hello'
print(s[0])
print(s[1])
print(s[-2])
测试结果:
h
e
l
- *切片:
切片规则:s[start : end:step] 从start开始到end-1结束,step:步长**
-
简单的切片
s = 'python' print(s[0:3]) print(s[0:4:2]) 测试结果: pyt pt
-
显示所有字符:print(s[:])
-
显示前3个字符:print(s[:3])
-
对字符串倒叙输出:print(s[::-1])
-
除了第一个字符以外,其他全部显示:print(s[1:])
s = 'python' print(s[:]) print(s[:3]) print(s[::-1]) print(s[1:]) 测试结果: python pyt nohtyp ython
-
*重复:运算符
s = 'python' print(s*5) 测试结果: pythonpythonpythonpythonpython
-
连接:+运算符
s = 'hetoto' print(s + ' ' 'I' ' ' 'love' ' ' 'you') 测试结果: hetoto I love you
-
成员操作符:in或not in
s = 'python'
print('h' in s)
print('t' not in s)
测试结果:
Ture
Fales
for循环(迭代)
s = 'python'
for i in s:
print(i)
测试结果:
p
y
t
h
o
n
三…字符串处理:
- 字符串判断数字:isdigit
判断字符串中每个元素为指定类型,其中只要有一个不满足就返回False
print('123'.isdigit())
print('123abc'.isdigit())
print('123C'.isdigit())
测试结果:
True
False
False
- 字符串判断某个字符串是否为标题(第一个字母大写,其余字母小写):istitle
print('Hello'.istitle())
print('HeLlo'.istitle())
测试结果:
True
False
- 字符串的转换及判断大小写:upper(大写)和lower(小写)
print('love'.upper())
print('LOVE'.isupper())
print('LOVE'.lower())
print('LOVE'.islower())
测试结果:
LOVE
True
love
False
- 字符串是否由字母和数字及只由字母组成:isalnum 和isalpha
print('hello123'.isalnum())
print('123'.isalpha())
print('aaa'.isalpha())
测试结果:
True
False
True
- 字符串开头和结尾的匹配:startswith(开头),endwith(结尾)
1.匹配结尾
filename = 'hello.log'
#如果filename以.log结尾打印filename,否则打印error
if filename.endswith('.log'):
print(filename)
else:
print('error filename')
测试结果:
hello.log
2.匹配开头
url1 = 'file:///mnt'
url2 = 'ftp://172.25.254.250/pub'
url3 = 'http://172.25.254.250/index.html'
if url3.startswith('http://'):
print('爬取该网页')
else:
print('错误网页')
测试结果:
爬取该网页
- 字符串去除两边空格:lstrip()左边空格,rstrip()右边空格
1.去除空格
s = ' hello '
#\t也表示空白字符相当于一个Tab键四个空格
s1 = '\thello\t'
print(s)
print(s.strip())
print(s.rstrip())
print(s.lstrip())
print(s1)
print(s.rstrip())
print(s.lstrip())
测试结果:
hello
hello
hello
hello
hello
hello
hello
2.去除指定字符:strip(‘指定字符’)
s = 'helloh1'
print(s.strip('h')) #去除字符串中的h字符
print(s.lstrip('he')) #去除最左边的he字符
print(s.rstrip('h1')) #去除最右边的h1字符
print(s.rstrip('o')) #因为最右边没有o字符所以字符串不变
测试结果:
elloh1
lloh1
hello
helloh1
- 字符串的搜索(find,rfind)与替换(replace)
1.find:找到子串,并返回最小的索引
2.rfind:找到子串,并返回最大索引
s = 'hello world hello'
print(s.find('hello'))
print(s.find('world'))
print(s.rfind('hello'))
测试结果:
0
6
12
3.replace:替换字符串中指定字符
s = 'hello world hello'
#替换字符串中所有的’hello‘为’westos‘
print(s.replace('hello','westos'))
测试结果:
westos world westos
- 字符串的对齐:center
print('学生管理系统'.center(30))
print('学生管理系统'.center(30,'*'))
print('学生管理系统'.center(30,'@'))
print('学生管理系统'.ljust(30,'*'))
print('学生管理系统'.rjust(30,'*'))
测试结果:
学生管理系统
************学生管理系统************
@@@@@@@@@@@@学生管理系统@@@@@@@@@@@@
学生管理系统************************
************************学生管理系统
- 字符串的统计:count
注:len是用来统计字符串长度的
print('hello'.count('l'))
print('hello'.count('ll'))
print(len('hello'))
测试结果:
2
1
5
- 字符串的分离和连接:split(分离),join(连接)
1.分离:split
s = '172.25.254.78'
s1 = s.split('.') #以.号为分隔符分离字符串s,默认为列表形式
print(s1)
print(s1[::-1]) #以.号为分隔符倒序输出
print(s[::-1])
date = '2019-03-26'
date1 = date.split('-')
print(date1)
测试结果:
['172', '25', '254', '78']
['78', '254', '25', '172']
87.452.52.271
['2019', '03', '26']
2.连接:join
date = '2019-03-26'
date1 = date.split('-')
print(date1)
#通过指定的字符进行链接
print(''.join(date1))
print('/'.join(date1))
测试结果:
['2019', '03', '26']
20190326
2019/03/26