编程小白学做题:Python 的经典编程题及详解,附代码和注释(七)
适合 Python 3 + 的 6 道编程练习题(附详解)
1. 检查字符串是否以指定子串开头
题目描述:判断字符串是否以给定子串开头(如"hello world"以"hello"开头)。
编写思路:
- 利用 Python 3 的字符串startswith()方法直接判断。
- 支持指定起始位置检查(如从索引 2 开始是否以"lo"开头)。
示例代码(Python 3+):
python
def starts_with(s, prefix, start=0):
return s.startswith(prefix, start)
# 测试用例
print(starts_with("hello", "he"))
# 输出: True
print(starts_with("hello", "lo", 3))
# 输出: True(从索引3开始检查)
print(starts_with("hello", "HE"))
# 输出: False(区分大小写)
代码注释:
- s.startswith(prefix, start):Python 3 字符串方法,start参数指定开始检查的索引位置。
- 大小写敏感:"A"和"a"被视为不同字符。
2. 检查字符串是否以指定子串结尾
题目描述:判断字符串是否以给定子串结尾(如"hello.txt"以".txt"结尾)。
编写思路:
- 使用 Python 3 的字符串endswith()方法。
- 支持多后缀检查(如endswith(('.txt', '.pdf')))。
示例代码(Python 3+):
python
def ends_with(s, suffix):
return s.endswith(suffix)
# 测试用例
print(ends_with("data.csv", ".csv"))
# 输出: True
print(ends_with("script.js", (".py", ".js")))
# 输出: True(多后缀检查)
print(ends_with("hello", "O"))
# 输出: False(大小写敏感)
代码注释:
- s.endswith(suffix):若suffix是元组,则检查是否以其中任一后缀结尾。
- 多后缀检查时,使用元组(suffix1, suffix2)格式。
3. 计算字符串中大写字母的数量
题目描述:统计字符串中大写字母(A-Z)的个数(如"Hello World"有 2 个大写字母)。
编写思路:
- 遍历字符串每个字符,判断是否为大写字母。
- 使用 Python 3 的字符串isupper()方法。
示例代码(Python 3+):
python
def count_uppercase(s):
count = 0
for char in s:
if char.isupper():
count += 1
return count
# 测试用例
print(count_uppercase("Python Programming"))
# 输出: 2
print(count_uppercase("123!@#"))
# 输出: 0
代码注释:
- char.isupper():Python 3 字符串方法,判断字符是否为大写字母(仅对 ASCII 字母有效)。
- 非字母字符(如数字、符号)调用isupper()返回False。
4. 计算字符串中小写字母的数量
题目描述:统计字符串中小写字母(a-z)的个数(如"Hello World"有 8 个小写字母)。
编写思路:
- 遍历字符串,使用islower()方法判断小写字母。
示例代码(Python 3+):
python
def count_lowercase(s):
count = 0
for char in s:
if char.islower():
count += 1
return count
# 测试用例
print(count_lowercase("Hello World"))
# 输出: 8
print(count_lowercase("PYTHON"))
# 输出: 0
代码注释:
- char.islower():判断字符是否为小写字母,非字母字符返回False。
5. 将列表中的元素按照长度排序
题目描述:将列表中的元素按其长度排序(如["apple", "cat", "banana"]排序为["cat", "apple", "banana"])。
编写思路:
- 使用 Python 3 的sorted()函数或列表sort()方法。
- 通过key=len指定按长度排序。
示例代码(Python 3+):
python
def sort_by_length(lst):
# 方法1:返回新列表(推荐)
return sorted(lst, key=len)
# 方法2:原地排序
# lst.sort(key=len)
# return lst
# 测试用例
words = ["apple", "cat", "banana", "dog"]
print(sort_by_length(words))
# 输出: ['cat', 'dog', 'apple', 'banana']
代码注释:
- sorted(lst, key=len):返回新列表,原列表不变。
- lst.sort(key=len):原地排序,修改原列表。
6. 找出字典中值最大的键
题目描述:找出字典中值最大的键(如{"a": 10, "b": 20, "c": 5}中值最大的键是"b")。
编写思路:
- 使用 Python 3 的max()函数结合key参数。
- 处理值相同的情况(返回第一个遇到的键)。
示例代码(Python 3+):
python
def find_key_with_max_value(d):
if not d: # 处理空字典
return None
return max(d, key=d.get)
# 测试用例
scores = {"Alice": 85, "Bob": 92, "Charlie": 78}
print(find_key_with_max_value(scores))
# 输出: 'Bob'
代码注释:
- max(d, key=d.get):key=d.get指定按值比较,返回键。
- 空字典处理:返回None,避免max()对空序列报错。
学习建议(Python 3 + 特性)
- 字符串方法:startswith()、endswith()、isupper()、islower()等字符串方法是 Python 3 处理文本的利器,熟练掌握可提升效率。
- 排序技巧:sorted()和sort()的key参数可接受函数(如len、str.lower),灵活定制排序规则。
- 字典操作:max(d, key=d.get)简洁高效地找出最大值对应的键,避免手动遍历。
这些 Python 3+ 练习题是否帮你巩固了基础?点赞收藏支持一下吧!后续会更新更多 Python 3 +练习题,更多的 Python3+ 进阶干货!关注我就能第一时间看到~你还想了解哪些 Python 3+ 特有的编程题目?欢迎在评论区留言!