当前位置:网站首页>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('不合法')
边栏推荐
- RDD和DataFrame和Dataset
- Requests the library deployment and common function
- 1068找到更多的硬币
- [cesium] element highlighting
- OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
- 02.01-----参数的引用的作用“ & ”
- How can Flutter parent and child components receive click events
- Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
- uboot enable debug printing information
- 有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
猜你喜欢

【过一下 17】pytorch 改写 keras
![[Go through 7] Notes from the first section of the fully connected neural network video](/img/e2/1107171b52fe9dcbf454f7edcdff77.png)
[Go through 7] Notes from the first section of the fully connected neural network video

【过一下7】全连接神经网络视频第一节的笔记

OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems

【过一下10】sklearn使用记录

DOM及其应用

Flutter real machine running and simulator running

vscode+pytorch使用经验记录(个人记录+不定时更新)

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

The mall background management system based on Web design and implementation
随机推荐
Lecture 4 Backpropagation Essays
『递归』递归概念与典型实例
UVA10827
pycharm中调用Matlab配置:No module named ‘matlab.engine‘; ‘matlab‘ is not a package
Requests the library deployment and common function
2022 Hangzhou Electric Multi-School 1st Session 01
jvm 三 之堆与栈
uva1325
Flutter learning - the beginning
span标签和p标签的区别
ESP32 485 Illuminance
Opencv中,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor‘
位运算符与逻辑运算符的区别
day10-字符串作业
学习总结week2_2
Detailed Explanation of Redis Sentinel Mode Configuration File
HQL语句执行过程
[Software Exam System Architect] Software Architecture Design ③ Domain-Specific Software Architecture (DSSA)
SQL(一) —— 增删改查
【读书】长期更新