当前位置:网站首页>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('不合法')
边栏推荐
- "PHP8 Beginner's Guide" A brief introduction to PHP
- CAP+BASE
- [cesium] element highlighting
- Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution
- [cesium] 3D Tileset model is loaded and associated with the model tree
- Flutter learning 2-dart learning
- 2022 Hangzhou Electric Multi-School 1st Session 01
- RL强化学习总结(一)
- Lecture 2 Linear Model Linear Model
- 【过一下12】整整一星期没记录
猜你喜欢
Flex layout frog game clearance strategy
Detailed Explanation of Redis Sentinel Mode Configuration File
Algorithms - ones and zeros (Kotlin)
2022 Hangzhou Electric Multi-School 1st Session 01
Basic properties of binary tree + oj problem analysis
[Go through 3] Convolution & Image Noise & Edge & Texture
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
Geek卸载工具
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!
第二讲 Linear Model 线性模型
随机推荐
Error creating bean with name 'configDataContextRefresher' defined in class path resource
Wise Force Deleter强制删除工具
DOM and its applications
uboot enable debug printing information
序列基础练习题
第四讲 back propagation 反向传播
重新审视分布式系统:永远不会有完美的一致性方案……
The underlying mechanism of the class
学习总结week2_4
vscode+pytorch use experience record (personal record + irregular update)
Machine Learning (2) - Machine Learning Fundamentals
Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction
jvm three heap and stack
Lecture 4 Backpropagation Essays
Returned object not currently part of this pool
分布式和集群
RDD和DataFrame和Dataset
Basic properties of binary tree + oj problem analysis
小白一枚各位大牛轻虐虐
【过一下14】自习室的一天