04-22(作业day6)

1.输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符)
例如: 输入'abcd1234 ' 输出'bd24'

str1 = input('请输入字符串:')
print(str1[1::2])

2.输入用户名,判断用户名是否合法(用户名长度6~10位)

value = input('请输入用户名:')
count = len(value)
if count < 6 or count > 10:
    print('不合法')
else:
    print('合法')

3.输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)

# 方法一:
value = input('请输入用户名:')
count1 = 0
count2 = 0
for char in value:
    count1 += 1
    if '0' <= char <= '9' or 'a' <= char <= 'z' or 'A' <= char <= 'Z':
        count2 += 1
if count1 == count2:
    print('合法')
else:
    print('不合法')
    
# 方法二:
value = input('请输入用户名:')
for char in value:
    if not('0' <= char <= '9' or 'a' <= char <= 'z' or 'A' <= char <= 'Z'):
        print('不合法')
        break
else:
    print('合法')

4.输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)

value = input('请输入用户名:')
count = 0
count1 = 0
count2 = 0
for char in value:
    if not'A' <= value[0] <= 'Z':
        break
    count += 1
    if '0' <= char <= '9':
        count1 += 1
    if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
        count2 += 1
if count == count1 + count2 and count1 > 0 and count2 >0:
    print('合法')
else:
    print('不合法')

5.输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串

value = input('请输入一个字符串:')
for char in value:
    if '0' <= char <= '9':
        print(char, end=' ')

6.输入一个字符串,将字符串中所有的小写字母变成对应的大写字母输出

value = input('请输入一个字符串:')
x1 = ''
for char in value:
    if 'a' <= char <= 'z':
        x1 += char
print(x1.upper())

7.输入一个小于1000的数字,产生对应的学号。例如: 输入'23',输出'py1901023' 输入'9', 输出'py1901009' 输入'123',输出'py1901123'

value = input('请输入数字:')
print('py1901', str(value).zfill(3), sep='')

8.输入一个字符串,统计字符串中非数字字母的字符的个数。例如: 输入'anc2+93-sj胡说' 输出:4 输入'===' 输出:3

value = input('请输入数字:')
count = 0
for char in value:
    if not('0' <= char <= '9' or 'a' <= char <= 'z' or 'A' <= char <= 'Z'):
        count += 1
print(count)

9.输入字符串,将字符串的开头和结尾变成'+',产生一个新的字符串。例如: 输入字符串'abc123', 输出'+bc12+'

str1 = input('请输入一个字符串:')
n = len(str1)-1
str1 = str1[1:n:]
print('+', str1, '+', sep='')

10.输入字符串,获取字符串的中间字符。例如: 输入'abc1234' 输出:'1' 输入'abc123' 输出'c1'

str1 = input('请输入一个字符串:')
n1 = len(str1)
n2 = int(n1/2)
if n1 % 2 == 0:
    print(str1[n2-1:n2+1:])
else:
    print(str1[n2:n2+1:])

你可能感兴趣的:(04-22(作业day6))