当前位置:网站首页>day9-字符串作业
day9-字符串作业
2022-08-05 05:13:00 【非鱼丶丶】
输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符)
例如: 输入’abcd1234 ’ 输出’bd24’
str1 = 'abcd1234'
for index, item in enumerate(str1):
if index % 2:
print(item, end='')
- 输入用户名,判断用户名是否合法(用户名长度6~10位)
user_name = input('请输入用户名:')
if 6 <= len(user_name) <= 10:
print('合法')
else:
print('不合法')
输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)
例如: ‘abc’ — 合法 ‘123’ — 合法 ‘abc123a’ — 合法
user_name = input('请输入用户名:')
for x in user_name:
if not ('0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z'):
print('不合法')
break
else:
print('合法')
输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串
例如:输入**‘abc1shj23kls99+2kkk’** 输出:‘123992’
str1 = input('请输入字符串:')
new_str = ''
for x in str1:
if '0' <= x <= '9':
new_str += x
print(new_str)
输入一个字符串,将字符串中所有的小写字母变成对应的大写字母输出 (用upper方法和自己写算法两种方式实现)
例如: 输入**‘a2h2klm12+’ ** 输出 ‘A2H2KLM12+’
str1 = 'a2h2klm12+'
str2 = str1.upper()
print(str2)
方法2
new_str = ''
for x in str1:
if 'a' <= x <= 'z':
x = chr(ord(x) - 32)
new_str += x
else:
new_str += x
print(new_str)
输入一个小于1000的数字,产生对应的学号
例如: 输入**‘23’,输出’py1901023’** 输入**‘9’, 输出’py1901009’** 输入**‘123’,输出’py1901123’**
num = input('请输入数字:')
if 0 <= int(num) <= 9:
print('py190100' + num)
elif 10 <= int(num) <= 99:
print('py19010' + num)
else:
print('py1901' + num)
输入一个字符串,统计字符串中非数字字母的字符的个数
例如: 输入**‘anc2+93-sj胡说’** 输出:4 输入**‘===’** 输出:3
num = input('请输入:')
count = 0
for x in num:
if not ('0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z'):
count += 1
print(count)
输入字符串,将字符串的开头和结尾变成’+',产生一个新的字符串
例如: 输入字符串**‘abc123’, 输出’+bc12+'**
num = 'abc123'
new_str = ''
for x in num:
if x == num[0] or x == num[-1]:
x = '+'
new_str += x
else:
new_str += x
print(new_str)
输入字符串,获取字符串的中间字符
例如: 输入**‘abc1234’** 输出:‘1’ 输入**‘abc123’** 输出**‘c1’**
num = 'abc1243'
if len(num) % 2:
print(num[len(num) // 2])
else:
print(num[len(num) // 2 - 1], num[len(num) // 2])
写程序实现字符串函数find/index的功能(获取字符串1中字符串2第一次出现的位置)
例如: 字符串1为:how are you? Im fine, Thank you! , 字符串2为:you, 打印8
获取两个字符串中公共的字符
例如: 字符串1为:abc123, 字符串2为: huak3 , 打印:公共字符有:a3
str1 = 'abc123'
str2 = 'huak3'
str3 = ''
for x in str1:
if x in str2:
str3 += x
print(str3)
输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)
例如: ‘abc’ — 不合法 ‘Mabc’ — 不合法 ‘123’ — 不合法 ‘abc123’ — 不合法 ‘Abc123ahs’ — 合法
user_name = input('请输入用户名:') if 'A' <= user_name[0] <= 'Z': for x in user_name: if not ('0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z'): print('不合法') break else: print('合法') else: print('不合法')
边栏推荐
- 1.3 mysql批量插入数据
- 2022牛客多校第四场C.Easy Counting Problem(EGF+NTT)
- NodeJs接收上传文件并自定义保存路径
- 第四讲 反向传播随笔
- What are the characteristics of the interface of the physical layer?What does each contain?
- CAP+BASE
- Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!
- How can Flutter parent and child components receive click events
- Using QR codes to solve fixed asset management challenges
- Flutter real machine running and simulator running
猜你喜欢

【过一下4】09-10_经典网络解析

数据库实验五 备份与恢复

Flutter真机运行及模拟器运行

Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!

SQL(二) —— join窗口函数视图

The underlying mechanism of the class

How can Flutter parent and child components receive click events

Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution

Lecture 4 Backpropagation Essays

【过一下6】机器视觉视频 【过一下2被挤掉了】
随机推荐
The mall background management system based on Web design and implementation
Geek卸载工具
【Untitled】
span标签和p标签的区别
多线程查询结果,添加List集合
Matplotlib(二)—— 子图
1.3 mysql batch insert data
使用二维码解决固定资产管理的难题
RL reinforcement learning summary (1)
u-boot in u-boot, dm-pre-reloc
[Go through 7] Notes from the first section of the fully connected neural network video
The difference between span tag and p
Judgment statement _switch and case
机器学习(二) —— 机器学习基础
number_gets the specified number of decimals
redis 缓存清除策略
Lecture 4 Backpropagation Essays
2022牛客多校第四场C.Easy Counting Problem(EGF+NTT)
MySQL Foundation (1) - Basic Cognition and Operation
jvm three heap and stack