-
输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符)
例如: 输入’abcd1234 ’ 输出’bd24’
str1 = 'abcd1234' print(str1[1::2])
-
输入用户名,判断用户名是否合法(用户名长度6~10位)
name = input('请输入用户名:') if 6 <= len(name) <= 10: print('合法') else: print('不合法')
4. 输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)
例如: ‘abc’ — 合法 ‘123’ — 合法 ‘abc123a’ — 合法
name = input('请输入用户名:')
flag = True
for x in name:
if not(x.isupper() or x.isdigit() or x.islower()):
flag = False
if flag == True:
print('合法')
else:
print('不合法')
# else!!!
username = 'daggesg'
for x in username:
if not(x.isupper() or x.isdigit() or x.islower()):
print(f'{username}不合法')
break
else:
print(f'{username}合法')
5. 输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串
例如:输入**‘abc1shj23kls99+2kkk’** 输出:‘123992’
str1 = input('输入字符串:')
str2 = ''
for x in str1:
if x.isdigit():
str2 += x
print(str2)
# 简便做法!!!
str1 = 'abc1shj23kls99+2kkk'
new_str = ''.join([x for x in str1 if x.isdigit()])
print(new_str)
-
输入一个字符串,将字符串中所有的小写字母变成对应的大写字母输出 (用upper方法和自己写算法两种方式实现)
例如: 输入**‘a2h2klm12+’ ** 输出 ‘A2H2KLM12+’
# 方法1:upper str1 = input('输入字符串:') print(str1.upper()) # 方法2: str1 = 'jdJIjk23' str2 = '' for x in str1: if 'a' <= x <= 'z': str2 += chr(ord(x)-(ord('a')-ord('A'))) else: str2 += x print(str2)
7. 输入一个小于1000的数字,产生对应的学号
例如: 输入**‘23’,输出’py1901023’** 输入**‘9’, 输出’py1901009’** 输入**‘123’,输出’py1901123’**
num = input('输入学号:')
if len(num) == 1:
num = '00' + num
if len(num) == 2:
num = '0' + num
print(f'py1901{num}')
# 简便的!
num = '23'
result = f'py1901{"0" * (3-len(num))}{num}'
#{表达式:符号>N}、{表达式:符号<N}、{表达式:符号^N}
x = 'abc'
# '0000abc'
result = f'{x:0>7}'
# 'abc0000'
result = f'{x:0<7}'
# '00abc00'
result = f'{x:0^7}'
# 'xxxxabc'
result = f'{x:x>7}'
-
输入一个字符串,统计字符串中非数字字母的字符的个数
例如: 输入**‘anc2+93-sj胡说’** 输出:4 输入**‘===’** 输出:3
str1 = input('输入字符串:') count = 0 for x in str1: if not (x.isdigit() or x.islower() or x.isupper()): count += 1 print(count)
-
输入字符串,将字符串的开头和结尾变成’+',产生一个新的字符串
例如: 输入字符串**‘abc123’, 输出’+bc12+'**
str1 = input('输入字符串:') print('+' + str1[1:-1] + '+') # print(f'+{str1[1:-1]}+')
-
输入字符串,获取字符串的中间字符
例如: 输入**‘abc1234’** 输出:‘1’ 输入**‘abc123’** 输出**‘c1’**
str1 = input('输入字符串:') if len(str1) % 2 == 0: print(str1[len(str1)//2-1:len(str1)//2+1]) else: print(str1[len(str1)//2])
-
写程序实现字符串函数find/index的功能(获取字符串1中字符串2第一次出现的位置)
例如: 字符串1为:how are you? Im fine, Thank you! , 字符串2为:you, 打印8
str1 = 'how are you? Im fine, Thank you!' str2 = 'you' for i in range(len(str1)-len(str2)+1): if str1[i:i+len(str2)] == str2: print(i) break else: print('找不到!')
12. 获取两个字符串中公共的字符
> 例如: 字符串1为:**abc123**, 字符串2为: **huak3** , 打印:**公共字符有:a3**
str1 = 'abc123'
str2 = 'ajidf123'
result = ''
for x in str1:
if x in str2:
result += x
print(result)
# 集合求交集
result = ''.join(set(str1) & set(str2))
print(result)
-
输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)
例如: ‘abc’ — 不合法 ‘Mabc’ — 不合法 ‘123’ — 不合法 ‘abc123’ — 不合法 ‘Abc123ahs’ — 合法
name = 'Abc123ahs'
count1 = 0
count2 = 0
for x in name:
if name[0].isupper():
if x.isupper() or x.islower():
count1 += 1
if x.isdigit():
count2 += 1
if count1 != 0 and count2 != 0 and count1+count2 == len(name):
print('合法')
else:
print('不合法')
# 老师的代码:
username = 'Khif23124'
if username[0].isupper():
num_count = 0
for x in username[1:]:
if not (x.isdigit() or x.islower() or x.isupper()):
print(f'{username}不合法')
break
elif x.isdigit():
num_count += 1
else:
if num_count == 0:
print(f'{username}不合法')
else:
print(f'{username}合法')
else:
print(f'{username}不合法')