"""字符串"""
# 1. 输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符)
# 例如: 输入'abcd1234' 输出'bd24'
str1 = 'abcd1234'
new_str = ''
for i in range(len(str1)):
if i % 2 != 0:
new_str += str1[i]
print(new_str)
print('-' * 40, 'next', '-' * 40)
# 2. 输入用户名,判断用户名是否合法(用户名长度6~10位)
user_name = input('请输入用户名:')
if 6 <= len(user_name) <= 10:
print('用户名合法')
else:
print('用户名不合法')
print('-' * 40, 'next', '-' * 40)
# 3. 输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)
# 例如: 'abc' — 合法 '123' — 合法 ‘abc123a’ — 合法
user_name2 = input('请输入用户名:')
count = 0
for i in user_name2:
if '0' <= i <= '9' or 'a' <= i <= 'z' or 'A' <= i <= 'Z':
count += 0
else:
count = 1
break
if count == 0:
print('合法')
else:
print('不合法')
print('-' * 40, 'next', '-' * 40)
# 4. 输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)
# 例如: 'abc' — 不合法 '123' — 不合法 'abc123' — 不合法 'Abc123ahs' — 合法 'Abc' — 不合法
user_name = '1Ajas'
num_count = 0
other_count = 0
if 'A' <= user_name <= 'Z':
for i in user_name:
if not ('0' <= i <= '9' or 'a' <= i <= 'z' or 'A' <= i <= 'Z'):
other_count += 1
break
else:
if '0' <= i <= '9':
num_count += 1
if num_count != 0 and other_count == 0:
print(user_name, '合法')
else:
print(user_name, '不合法')
else:
print(user_name, '不合法')
print('-'
Python基础小练习_字符串和字典1
于 2023-02-11 14:11:22 首次发布