Pyhton字符串的搜索、替换、对齐、统计、分离、连接、反转
1、搜索
#搜索
s = "school class student"
print(s.find("sc")) #返回最小索引值
print(s.rfind("ool")) #返回最大索引值
# 替换
print(s.replace("school","university"))
#对齐
print("26岁的年纪扬帆起航".center(30)) #居中对齐左右30空格
print("26岁的年纪扬帆起航".center(30,'*')) #居中对齐左右30个*
print("26岁的年纪扬帆起航".ljust(30,'*')) #左对齐补30个*
print("26岁的年纪扬帆起航".rjust(30,'*')) #右对齐补30个*
# 统计
print("helloworld".count('o'))
print("helloworld".count('l'))
# 分离
print("helloworld".split('w'))
print("127.0.0.1".split('.'))
# 以什么进行连接
IP = "127.0.0.1"
s = IP.split('.')
print(s)
v = '.'.join(s)
k = '@'.join(s)
print(v)
print(k)
# 反转
print(' '.join(input("输入:").split()[::-1]))
2、题目
# 给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符:
# 'A' : Absent,缺勤
# 'L' : Late,迟到
# 'P' : Present,到场
# 如果一个学生的出勤纪录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),那么这个学生会被奖赏。
# 你需要根据这个学生的出勤纪录判断他是否会被奖赏。
# 示例 1:
# 输入: "PPALLP"
# 输出: True
# 示例 2:
# 输入: "PPALLL"
# 输出: False
while True:
num = input("输入考勤代码:")
if num.count('A') < 2 and num.count('LLL') < 1:
print("该学生考勤合格")
else:
print('该学生考勤不合格')