【编程小白必看】Python编程练习题字符串操作秘籍🔥一文全掌握
文章目录
- 前言
- 一、练习题精选
- 1.输出字符串
- 2.字符串长度
- 3.字符串切片
- 4.字符串拼接
- 5.字符串重复
- 6.字符串查找子串
- 7.字符串替换
- 8.字符串分割
- 9.字符串大小写转换
- 10.字符串去除首尾空格
- 11.字符串去除左侧空格
- 12.字符串去除右侧空格
- 13.字符串格式化
- 14.字符串格式化(老版本)
- 15.字符串格式化(%操作符)
- 16.字符串反转
- 17.字符串去重
- 18.字符串转列表(去重)
- 19.字符串转集合
- 20.字符串转列表(保留顺序)
- 21.字符串转列表(去重并保留顺序)
- 22.字符串转列表(去重并排序)
- 23.字符串转列表(去重并排序,保留原顺序)
- 24.字符串是否为回文
- 25.字符串是否包含数字
- 26.字符串是否包含字母
- 27.字符串是否包含大写字母
- 28.字符串是否包含小写字母
- 29.字符串是否全为字母
- 30.字符串是否全为数字
- 总结
前言
大家好!今天给大家带来一份Python字符串操作练习题精选,特别针对初学者设计。通过这些练习题,你可以更好地掌握Python中字符串的基本操作和常用方法。无论是刚开始学习编程的新手,还是希望巩固基础知识的朋友,都可以从中受益。
一、练习题精选
1.输出字符串
代码如下(示例):
s = "Hello, World!"
print(s)
2.字符串长度
代码如下(示例):
s = "Hello, World!"
length = len(s)
print(length)
3.字符串切片
代码如下(示例):
s = "Hello, World!"
substring = s[0:5]
print(substring)
4.字符串拼接
代码如下(示例):
part1 = "Hello"
part2 = ", World!"
result = part1 + part2
print(result)
5.字符串重复
代码如下(示例):
s = "Hi"
repeated = s * 3
print(repeated)
6.字符串查找子串
代码如下(示例):
s = "Hello, World!"
substring = "World"
index = s.find(substring)
print(index)
7.字符串替换
代码如下(示例):
s = "Hello, World!"
new_s = s.replace("World", "Python")
print(new_s)
8.字符串分割
代码如下(示例):
s = "apple,banana,orange"
fruits = s.split(',')
print(fruits)
9.字符串大小写转换
代码如下(示例):
s = "hello world"
upper_s = s.upper()
lower_s = s.lower()
print(upper_s, lower_s)
10.字符串去除首尾空格
代码如下(示例):
s = " hello world "
trimmed_s = s.strip()
print(trimmed_s)
11.字符串去除左侧空格
代码如下(示例):
s = " hello world "
trimmed_s = s.lstrip()
print(trimmed_s)
12.字符串去除右侧空格
代码如下(示例):
s = " hello world "
trimmed_s = s.rstrip()
print(trimmed_s)
13.字符串格式化
代码如下(示例):
name = "Alice"
age = 25
formatted_s = f"My name is {name} and I am {age} years old."
print(formatted_s)
14.字符串格式化(老版本)
代码如下(示例):
name = "Alice"
age = 25
formatted_s = "My name is {} and I am {} years old.".format(name, age)
print(formatted_s)
15.字符串格式化(%操作符)
代码如下(示例):
name = "Alice"
age = 25
formatted_s = "My name is %s and I am %d years old." % (name, age)
print(formatted_s)
16.字符串反转
代码如下(示例):
s = "hello"
reversed_s = s[::-1]
print(reversed_s)
17.字符串去重
代码如下(示例):
s = "hello world"
unique_s = ''.join(sorted(set(s), key=s.index))
print(unique_s)
18.字符串转列表(去重)
代码如下(示例):
s = "hello world"
unique_list = list(set(s))
print(unique_list)
19.字符串转集合
代码如下(示例):
s = "hello world"
set_s = set(s)
print(set_s)
20.字符串转列表(保留顺序)
代码如下(示例):
s = "hello world"
list_s = list(s)
print(list_s)
21.字符串转列表(去重并保留顺序)
代码如下(示例):
s = "hello world"
unique_list = []
for char in s:
if char not in unique_list:
unique_list.append(char)
print(unique_list)
22.字符串转列表(去重并排序)
代码如下(示例):
s = "hello world"
sorted_unique_list = sorted(set(s))
print(sorted_unique_list)
23.字符串转列表(去重并排序,保留原顺序)
代码如下(示例):
s = "hello world"
sorted_unique_list = sorted(set(s), key=s.index)
print(sorted_unique_list)
24.字符串是否为回文
代码如下(示例):
s = "racecar"
is_palindrome = s == s[::-1]
print(is_palindrome)
25.字符串是否包含数字
代码如下(示例):
s = "hello123"
has_digit = any(char.isdigit() for char in s)
print(has_digit)
26.字符串是否包含字母
代码如下(示例):
s = "hello123"
has_letter = any(char.isalpha() for char in s)
print(has_letter)
27.字符串是否包含大写字母
代码如下(示例):
s = "Hello123"
has_upper = any(char.isupper() for char in s)
print(has_upper)
28.字符串是否包含小写字母
代码如下(示例):
s = "Hello123"
has_lower = any(char.islower() for char in s)
print(has_lower)
29.字符串是否全为字母
代码如下(示例):
s = "Hello"
is_alpha = s.isalpha()
print(is_alpha)
30.字符串是否全为数字
代码如下(示例):
s = "123"
is_digit = s.isdigit()
print(is_digit)
总结
通过以上练习题,你可以逐步熟悉Python中的字符串操作,并且能够应对实际开发中常见的字符串处理任务。希望这些练习对你有所帮助!如果你有任何问题或建议,请随时留言交流。祝你学习愉快!