字符串练习一:判断回文数
num = input('Num:')
if num == num[::-1]:
print('回文数')
else:
print('不是')
字符串练习二:判断变量名是否合法
"""
1.字母、数字、下划线
2.只能以字母或者下划线开头
s = 'hello#'
"""
"""
1.第一个字符是否为字母或者下划线
2.如果是,继续
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)
字符串练习三:判断学生出勤
"""
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个
字符:
'A' : Absent,缺勤
'L' : Late,迟到
'P' : Present,到场
如果一个学生的出勤纪录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),
那么这个学生会被奖赏。
你需要根据这个学生的出勤纪录判断他是否会被奖赏。
示例 1:
输入: "PPALLP"
输出: True
示例 2:
输入: "PPALLL"
输出: False
"""
方法一:
s = input()
if s.count('A') <= 1 and s.count('LLL') == 0:
print(True)
else:
print(False)
方法二:
s = input()
print(s.count('A') <= 1 and s.count('LLL') == 0)
字符串练习四:字符串的反转与替换
"""
hello westos python
python westos hello
"""
str=input("请输入字符串:")
str1 = str.split()
print(' '.join(str1[::-1]))
"""
字符的替换
they are students
删掉aeiou
thy r stdnts
"""
str=input("请输入字符串:")
str1=input("请输入替换的字符:")
for i in str1:
str = str.replace(i,'')
print(str)