值得收藏的 Python 练手题

30道Python练手题

1. 已知一个字符串为 “hello_world_yoyo”,如何得到一个队列 [“hello”,”world”,”yoyo”] ?

# 使用split函数,分割字符串,并且将数据转换成列表类型:
test = 'htllo_world_yoyo'

print(test.split("_"))

2. 有个列表 [“hello”, “world”, “yoyo”],如何把列表里面的字符串联起来,得到字符串 “hello_world_yoyo”?

# 使用join函数将数据转换成字符串:
test = ['htllo', 'world', 'yoyo']

print("_".join(test))

使用for循环拼接如下:

test = ['htllo', 'world', 'yoyo']
# 定义一个空字符串
j = ''
# 通过for循环打印出列表中的数据
for i in test:
    j = j + "_" + i
    # 因为通过上面的字符串拼接,得到的数据是“_hello_world_yoyo”,前面会多一个下划线_,所以把这个下划线去掉
    print(j.lstrip("_"))

3. 把字符串 s 中的每个空格替换成”%20”,输入:s = “We are happy.”,输出:“We%20are%20happy.”。

# 使用replace函数,替换字符换即可:
s = "We are happy."

print(s.replace(' ', '%20'))

4. python如何打印 99 乘法表

# for 循环打印:
for i in range(1, 10):
    for j in range(1, i + 1):
        print('{}x{}={}\t'.format(j, i, i * j), end='')
    print()
# 使用while循环实现:
i = 1
while i <= 9:
    j = 1
    while j <= i:
        print("%dx%d=%-2d" % (i, j, i * j), end=' ')  # %d: 整数的占位符,'-2'代表靠左对齐,两个占位符
        j += 1
    print()
    i += 1

5. 从下标0开始索引,找出单词“welcome”在字符串“Hello,welcome to my world.”中出现的位置,找不到返回 -1。

结果为 7
def test():
    message = 'Hello, welcome to my world.'
    world = 'welcome'
    if world in message:
        return message.find(world)
    else:
        return -1

print(test())

6. 统计字符串“Hello, welcome to my world.” 中字母 w 出现的次数。

# 结果为:2 次
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 num

print(test())

7. 输入一个字符串 str,输出第 m 个只出现过 n 次的字符,如在字符串 gbgkkdehh 中,找出第 2 个只出现 1 次的字符,输出结果:d

def test(str_test, num, counts):
    '''
    :param str_test: 字符串
    :param num: 字符串出现的次数
    :param counts: 字符串第几次出现的次数
    :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))

8. 判断字符串 a = “welcome to my world” 是否包含单词 b = “world”,包含返回 True,不包含返回 False。

# 结果为 True
def test():
    message = 'welcome to my world'
    world = 'world'

    if world in message:
        return True
    return False

print(test())

9. 从 0 开始计数,输出指定字符串 A = “hello” 在字符串 B = “hi how are you hello world, hello yoyo!”中第一次出现的位置,如果 B 中不包含 A,则输出 -1。

# 输出结果为:15
def test():
    message = 'hi how are you hello world, hello yoyo!'
    world = 'hello'

    return message.find(world)

print(test())

10.从 0 开始计数,输出指定字符串 A = “hello”在字符串 B = “hi how are you hello world, hello yoyo!”中最后出现的位置,如果 B 中不包含 A,则输出 -1。

def test(string, str):
    # 定义 last_position 初始值为 -1
    last_posistion = -1
    while True:
        position = string.find(str, last_posistion + 1)
        if position == -1:
            return last_posistion
        last_posistion = position

print(test('hi how are you hello world, hello yoyo!', 'hello'))

11. 给定一个数 a,判断一个数字是否为奇数或偶数。

while True:
    try:
        # 判断输入是否为整数
        num = int(input('输入一个整数:'))
    # 不是纯数字需要重新输入
    except ValueError:
        print('输入的不是整数!')
        continue
    # 整除,%为取余数,就是除下来以后余多少
    if num % 2 == 0:
        print('偶数')
    else:
        print('奇数')
    break

12. 输入一个姓名,判断是否姓王。

def test():
    user_input = input('请输入您的姓名:')
    if user_input[0] == '王':
        return "用户姓王"
    return "用户不姓王"

print(test())

13. 如何判断一个字符串是不是纯数字组成?

# 利用 Python 提供的类型转行,将用户输入的数据转换成浮点数类型,如果转换抛异常,则判断数字不是纯数字组成。

```python
def test(num):
    try:
        return float(num)
    except ValueError:
        return "请输入数字"

print(test('133w3'))

14. 将字符串 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(a.lower())  # 大写字母转换成小写

15. 将字符串 a = “ welcome to my world ”首尾空格去掉

# Python 提供了strip() 方法,可以去除首尾空格,rstrip() 去掉尾部空格,lstrip() 去掉首部空格,replace(" ", “”) 去掉全部空格。
a = '  welcome to my world   '

print(a.strip())

16. 将字符串 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())

17. 打印出如下图案(菱形):

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())
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值