最近为了提升python水平,吐血题目在网上找到了python习题,整理最全然后根据自己对于python的习题掌握,整理出来了答案,道含答案如果小伙伴们有更好的持续实现方式,可以下面留言大家一起讨论哦~
- 已知一个字符串为 “hello_world_yoyo”,更新 如何得到一个队列 [“hello”,”world”,”yoyo”]
test = 'hello_world_yoyo'# 使用split函数,分割字符串,建议并且将数据转换成列表类型print(test.split("_"))结果:['hello',收藏 'world', 'yoyo']Process finished with exit code 0
- 有个列表 [“hello”, “world”, “yoyo”]如何把把列表里面的字符串联起来,得到字符串 “hello_world_yoyo”
test = ["hello",吐血题目 "world", "yoyo"]# 使用 join 函数将数据转换成字符串print("_".join(test))结果:hello_world_yoyoProcess finished with exit code 0
这边如果不依赖python提供的join方法,我们还可以通过for循环,整理最全然后将字符串拼接,习题但是道含答案在用"+"连接字符串时,结果会生成新的持续对象,
用join时结果只是更新将原列表中的元素拼接起来,所以join效率比较高
test = ["hello",建议 "world", "yoyo"]# 定义一个空字符串j = ''# 通过 for 循环 打印出列表中的数据for i in test: j = j + "_" + i# 因为通过上面的字符串拼接,得到的数据是“_hello_world_yoyo”,前面会多一个下划线,所以我们下面把这个下划线去掉print(j.lstrip("_"))
- 把字符串 s 中的每个空格替换成”%20”
输入:s = “We are happy.”
输出:”We%20are%20happy.”
s = 'We are happy.'print(s.replace(' ', '%20'))结果:We%20are%20happy.Process finished with exit code 0
- 打印99乘法表
for i in range(1, 10): for j in range(1, i+1): print('{ }x{ }={ }\t'.format(j, i, i*j), end='') print()结果:1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 Process finished with exit code 0
下面是使用while循环实现
i = 1while i <= 9: j = 1 while j <= i: print("%d*%d=%-2d"%(i,j,i*j),end = ' ') # %d: 整数的占位符,'-2'代表靠左对齐,两个占位符 j += 1 print() i += 1
- 找出单词 “welcome” 在 字符串”Hello, welcome to my world.” 中出现的位置,找不到返回-1
从下标0开始索引
def test(): message = 'Hello, welcome to my world.' world = 'welcome' if world in message: return message.find(world) else: return -1print(test())结果:7Process finished with exit code 0
- 统计字符串“Hello, welcome to my world.” 中字母w出现的次数
统计单词 my 出现的次数
def test(): message = 'Hello, welcome to my world.' # 计数 num = 0 # for 循环message for i in message: # 判断如果 ‘w’ 字符串在 message中,则num +1 if 'w' in i: num += 1 return numprint(test())
- 题目:输入一个字符串str, 输出第m个只出现过n次的字符,如在字符串 gbgkkdehh 中,
找出第2个只出现1 次的字符,输出结果:d
def test(str_test, num, counts): """ :param str_test: 字符串 :param num: 字符串出现的次数 :param count: 字符串第几次出现的次数 :return: """ # 定义一个空数组,存放逻辑处理后的数据 list = [] # for循环字符串的数据 for i in str_test: # 使用 count 函数,统计出所有字符串出现的次数 count = str_test.count(i, 0, len(str_test)) # 判断字符串出现的次数与设置的counts的次数相同,则将数据存放在list数组中 if count == num: list.append(i) # 返回第n次出现的字符串 return list[counts-1]print(test('gbgkkdehh', 1, 2))结果:dProcess finished with exit code 0
- 判断字符串a=”welcome to my world” 是否包含单词b=”world”
包含返回True,不包含返回 False
def test(): message = 'welcome to my world' world = 'world' if world in message: return True return Falseprint(test())结果:TrueProcess finished with exit code 0
- 输出指定字符串A在字符串B中第一次出现的位置,如果B中不包含A,则输出-1
从 0 开始计数
A = “hello”
B = “hi how are you hello world, hello yoyo !”
def test(): message = 'hi how are you hello world, hello yoyo !' world = 'hello' return message.find(world)print(test())结果:15Process finished with exit code 0
- 输出指定字符串A在字符串B中最后出现的位置,如果B中不包含A, 出-1从 0 开始计数
A = “hello”
B = “hi how are you hello world, hello yoyo !”
def test(string, str): # 定义 last_position 初始值为 -1 last_position = -1 while True: position = string.find(str, last_position+1) if position == -1: return last_position last_position = positionprint(test('hi how are you hello world, hello yoyo !', 'hello'))结果:28Process finished with exit code 0
- 给定一个数a,判断一个数字是否为奇数或偶数
a1 = 13
a2 = 10
while True: try: # 判断输入是否为整数 num = int(input('输入一个整数:')) # 不是纯数字需要重新输入 except ValueError: print("输入的不是整数!") continue if num % 2 == 0: print('偶数') else: print('奇数') break结果:输入一个整数:100偶数Process finished with exit code 0
- 输入一个姓名,判断是否姓王
a = “王五”
b = “老王”
def test(): user_input = input("请输入您的姓名:") if user_input[0] == '王': return "用户姓王" return "用户不姓王"print(test())结果:请输入您的姓名:王总用户姓王Process finished with exit code 0
- 如何判断一个字符串是不是纯数字组成
a = “123456”
b = “yoyo123”
这个答案,其实有些取巧,利用python提供的类型转行,将用户输入的数据转换成浮点数类型,如果转换抛异常,则判断数字不是纯数字组成
def test(num): try: return float(num) except ValueError: return "请输入数字"print(test('133w3'))
- 将字符串 a = “This is string example….wow!” 全部转成大写
字符串 b = “Welcome To My World” 全部转成小写
a = 'This is string example….wow!'b = 'Welcome To My World'print(a.upper())print(b.lower())
- 将字符串 a = “ welcome to my world “首尾空格去掉
python提供了strip()方法,可以去除首尾空格
rstrip()去掉尾部空格
lstrip()去掉首部空格
replace(" ", “”) 去掉全部空格
a = ' welcome to my world 'print(a.strip())
还可以通过递归的方式实现
def trim(s): flag = 0 if s[:1]==' ': s = s[1:] flag = 1 if s[-1:] == ' ': s = s[:-1] flag = 1 if flag==1: return trim(s) else: return sprint(trim(' Hello world! '))
通过while循环实现
def trim(s): while(True): flag = 0 if s[:1]==' ': s = s[1:] flag = 1 if s[-1:] == ' ': s = s[:-1] flag = 1 if flag==0: break return sprint(trim(' Hello world! '))
- s = “ajldjlajfdljfddd”,去重并从小到大排序输出”adfjl”
def test(): s = 'ajldjlajfdljfddd' # 定义一个数组存放数据 str_list = [] # for循环s字符串中的数据,然后将数据加入数组中 for i in s: # 判断如果数组中已经存在这个字符串,则将字符串移除,加入新的字符串 if i in str_list: str_list.remove(i) str_list.append(i) # 使用 sorted 方法,对字母进行排序 a = sorted(str_list) # sorted方法返回的是一个列表,这边将列表数据转换成字符串 return "".join(a)print(test())结果:adfjlProcess finished with exit code 0
- 题目 打印出如下图案(菱形):
def test(): n = 8 for i in range(-int(n/2), int(n/2) + 1): print(" "*abs(i), "*"*abs(n-abs(i)*2))print(test())结果: ** ****