1.字符串的定义
举例说明字符串的定义
a = "hello"
b = 'westos'
c = "what's up"
d = 'what\'s up'
e = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
"""
print(a)
print(b)
print(c)
print(d)
print(e)
print(type(e))
运行结果:
2.字符串的特性
索引
索引: 0,1,2,3,4 索引值默认从0开始
s = ‘hello’print(s[1])
print(s[0])
切片
切片
print(s[0:3])
切片的规则: s[start?step] 从start开始,到end-1结束,步长:stepprint(s[0:4:2])
#显示所有字符
print(s[:])
#显示前3个字符
print(s[:3])
#字符串逆序输出
print(s[::-1])
#除了第一个字符以外,其他全部输出
print(s[1:])
重复
print( s * 10)
连接
print('hello' + ' ' + 'world')
成员操作符
输出结果为布尔值
print(‘h’ in s)
print(‘q’ in s)
3.回文数判断
num = input('Num:')
if num == num[::-1]:
print('回文数')
else:
print('不是')
4.字符串判断
判断字符串里面每个元素是否为某种类型
print(‘123’.isdigit())
print(‘123abc’.isdigit())
title:首字母大写,其余字母小写
print(‘Hello’.istitle())
print(‘HeLlo’.istitle())
# print('hello'.upper()) 改为大写,输出结果为HELLO
# print('hello'.isupper()) 判断是不是大写,输出结果为False
# print('hElLo'.lower()) 改为小写,输出结果为hello
# print('hElLo'.islower()) 判断是不是小写,输出结果为False
# print('hello123'.isalnum()) 判断是不是数字字母组合,输出结果是True
# print('123'.isalpha()) 判断是不是字母,输出结果为False
# print('abc'.isalpha()) 输出结果为True
print(isinstance(1,int)) 判断1是不是int类型,输出结果为True
print(isinstance('a',str)) 判断a是不是str类型,输出结果为True
print(isinstance(1,str)) 判断1是不是str类型,输出结果为False
5.字符串去掉开头和结尾
s = ' hello '
print(s.strip()) #去掉两边的空格
print(s.lstrip()) #去掉左边的空格
print(s.rstrip()) #去掉右边的空格
s = ' hello \nworld '
print(s.strip()) #去掉空格,换行
s = 'westos\thello '
print(s.strip()) #去掉空格,制表
s = 'helloh'
print(s.strip('h')) #去掉h
print(s.lstrip('he')) #去掉左边的he
6.字符串匹配开头和结尾
匹配开头
url1 = 'file:///mnt'
url2 = 'ftp://172.25.254.254/pub'
url3 = 'http://172.25.254.254/index.html'
if url3.startswith('http://'):
print('爬取该网页')
else:
print('错误网页')
匹配结尾:
filename = 'hello.log'
if filename.endswith('.log'):
print(filename)
else:
print('error')
7.使用字符串判断变量名是否合法
变量名是否合法:
1.变量名只能由字母、数字、下划线组成
2.只能以字母或下划线开头
1.变量名第一个字符是否为字母或者下划线
2.如果是,继续 --> 4
3.如果不是,报错 , 退出
4.依次判断除了第一个字符以外的其他字符
5.判断是否为字母数字或者下划线
while True:
s = input('变量名:')
if s == 'exit':
print('欢迎下次使用')
break
if s[0].isalpha() or s[0] == '_':
for i in s[1:]:
if not (i.isalnum() or i == '_'):
print('%s变量名不合法' %s)
break
else:
print('%s变量名合法' %s)
else:
print('%s变量名不合法' %s)
8.字符串的搜索和替换
s = 'hello world hello'
#find找到子串,并返回最小的索引
print(s.find('hello'))
print(s.find('world'))
#rfind找到子串,并返回最大的索引值
print(s.rfind('hello'))
#替换字符串中所有的‘hello’为‘westos’
print(s.replace('hello','westos'))
9.字符串的对齐
print('字符串的对齐'.center(30))
print('字符串的对齐'.center(30,'*'))
print('字符串的对齐'.center(30,'@'))
print('字符串的对齐'.ljust(30,'*'))
print('字符串的对齐'.rjust(30,'*'))
10.字符串的统计
print('hello'.count('l'))
print('hello'.count('ll'))
print(len('hello'))
11.字符串的分离连接
s = '172.25.254.254'
s1 = s.split('.')
print(s1)
print(s1[::-1])
date = '2019-02-18'
date1 = date.split('-')
print(date1)
print(''.join(date1))
print('/'.join(date1))
12.字符串的练习
<1>
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以
下三个字符:
‘A’ : Absent,缺勤
‘L’ : Late,迟到
‘P’ : Present,到场
如果一个学生的出勤纪录中不超过一个’A’(缺勤)并且不超过两>个连续的’L’(迟到),
那么这个学生会被奖赏。
你需要根据这个学生的出勤纪录判断他是否会被奖赏。
示例 1:
输入: “PPALLP”
输出: True
示例 2:
输入: “PPALLL”
输出: False
kaoqin = input("某学生的考勤输入:")
if (kaoqin.count('A') <= 1 and kaoqin.count('LLL') <= 0):
print('奖赏')
else:
print('不奖赏')
<2>
输入
hello xiao mi
输出
mi xiao hello
print(' '.join(input().split()[::-1]))