仅需10道题轻松掌握Python字符串方法 | Python技能树征题
Posted 盼小辉丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅需10道题轻松掌握Python字符串方法 | Python技能树征题相关的知识,希望对你有一定的参考价值。
仅需10道题轻松掌握Python字符串方法 | Python技能树征题
0.前言
文本处理在几乎所有应用程序中都会有所涉及,例如文本的搜索、替换和删除等操作,为了方便的处理这些常见问题,Python
字符串内建了相关方法,我们就通过 10
道编程题来掌握这些常用的 Python
字符串方法吧!
1. 第 1 题:字符串检查
知识点描述:Python
字符串内建了多种方法用于检测字符类型是字母、数字或者其他类型。
问题描述:请从以下选项中选出输出为 True 的选项:
A.
string = '11.2'
result = string.isdigit()
print(result)
B.
string = '四十'
result = string.isnumeric()
print(result)
C.
string = '11.2'
result = string.isnumeric()
print(result)
D.
string = '四十'
result = string.isdigit()
print(result)
正确答案: B
2. 第 2 题:字符串大小写转换
知识点描述:将字符串中的字母进行大小写的转换是文本处理中常见的操作。
问题描述:将字符串中每个单词首字母转换为大写字母,请从以下选项中选出你认为正确的答案:
A.
string = 'Learn PYTHON from beginner to exPERT!'
result = string.upper()
print(result)
B.
string = 'Learn PYTHON from beginner to exPERT!'
result = string.capitalize()
print(result)
C.
string = 'Learn PYTHON from beginner to exPERT!'
result = string.lower().capitalize()
print(result)
D.
string = 'Learn PYTHON from beginner to exPERT!'
result = string.title()
print(result)
正确答案: D
3. 第 3 题:字符串开头或结尾匹配
知识点描述:通过指定的文本模式检查字符串的开头或者结尾。
问题描述:请从以下选项中选出输出为 True 的选项:
A.
choices = 'http:'
url = "https://blog.csdn.net/LOVEmy134611"
result = url.startswith(choices)
print(result)
B.
choices = ['http:', 'https:']
url = "https://blog.csdn.net/LOVEmy134611"
result = url.startswith(choices)
print(result)
C.
choices = ['http:', 'https:']
url = "https://blog.csdn.net/LOVEmy134611"
result = url.startswith(tuple(choices))
print(result)
D.
choices = 'https:'
url = "https://blog.csdn.net/LOVEmy134611"
result = url.endswith(choices)
print(result)
正确答案: C
4. 第 4 题:字符串匹配和搜索
知识点描述:匹配或者搜索特定模式的文本。
问题描述:请从以下选项中选出能够正确搜索 “Python” 第一次出现位置的答案:
A.
string = "Python is very simple, Let's learn Python together"
result = string.startswith('Python')
print(result)
B.
string = "Python is very simple, Let's learn Python together"
result = string.rfind('Python')
print(result)
C.
string = "Python is very simple, Let's learn Python together"
result = string.find('Python')
print(result)
D.
string = "Python is very simple, Let's learn Python together"
result = string.find('python')
print(result)
正确答案: C
5. 第 5 题:字符串替换
知识点描述:在字符串中搜索和匹配指定的文本,并进行使用其它字符进行替换。
问题描述:请从以下选项中选出能够正确将第一次出现的 “better” 替换为 “worse” 的答案:
A.
string = """Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex."""
index = string.find('better')
result = string.replace('better', 'worse', 1)
print(result)
B.
string = """Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex."""
index = string.find('better')
result = string.replace('better', 'worse', 0)
print(result)
C.
string = """Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex."""
index = string.find('better')
result = string.replace('better', 'worse', -1)
print(result)
D.
string = """Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex."""
result = string.replace('better', 'worse')
print(result)
正确答案:A
6. 第 6 题:删除字符串中不需要的字符
知识点描述:去掉文本字符串开头,结尾或者中间不需要的字符。
问题描述:请从以下选项中选出能够正确去除开头和结尾空白字符(空白字符包括空格、换行符和 tab 键)的选项:
A.
string = " \\n Beautiful is better than ugly. \\n "
result = string.replace('\\n', '')
print(result)
B.
string = " \\n Beautiful is better than ugly. \\n "
result = string.rstrip()
print(result)
C.
string = " \\n Beautiful is better than ugly. \\n "
result = string.strip(' ')
print(result)
D.
string = " \\n Beautiful is better than ugly. \\n "
result = string.lstrip().rstrip()
print(result)
正确答案:D
7. 第 7 题:字符串对齐
知识点描述:通过指定的对齐方式来格式化字符串。
问题描述:将给定字符串居中,并指定返回字符串长度为40,使用等号("=")进行填充,请从以下选项中选出你认为正确的答案:
A.
string = "Beautiful is better than ugly."
result = string.center(len(string)+40, '=')
print(result)
B.
string = "Beautiful is better than ugly."
result = string.center(40, '=')
print(result)
C.
string = "Beautiful is better than ugly."
result = string.rjust(40,'=')
print(result)
D.
string = "Beautiful is better than ugly."
result = string.ljust(40,'=')
print(result)
正确答案:B
8. 第 8 题:字符串分割
知识点描述:将一个大的字符串分割为若干个小的字符串。
问题描述:将给定字符串划分为单词,并将单词首字母大写,请从以下选项中选出你认为正确的答案:
A.
string = 'Beautiful is better than ugly.'
result = string.split()
result = [i.strip('.') for i in result]
print(result)
B.
string = 'Beautiful is better than ugly.'
result = string.split()
result = [i.title() for i in result]
print(result)
C.
string = 'Beautiful is better than ugly.'
result = string.split()
result = [i.title().strip('.') for i in result]
print(result)
D.
string = 'Beautiful is better than ugly.'
result = string.split()
result = [i.strip('.').upper() for i in result]
print(result)
正确答案:C
9. 第 9 题:字符串的合并拼接
知识点描述:将几个小的字符串合并为一个大的字符串。
问题描述:将列表中元素合并为一个大的字符串,请从以下选项中选出你认为正确的答案:
A.
list_part = ['The', 'area', 'of', 'the', 'school', 'is', 1.7, 'million', 'square', 'meters.']
result = ' '.join(list_part)
print(result)
B.
list_part = ['The', 'area', 'of', 'the', 'school', 'is', 1.7, 'million', 'square', 'meters.']
result = ' '.join(str(i) for i in list_part)
print(result)
C.
list_part = ['The', 'area', 'of', 'the', 'school', 'is', 1.7, 'million', 'square', 'meters.']
result = ''.join(list_part)
print(result)
D.
list_part = ['The', 'area', 'of', 'the', 'school', 'is', 1.7, 'million', 'square', 'meters.']
result = ''.join(str(i) for i in list_part)
print(result)
正确答案:B
10. 第 10 题:在字符串中插入变量
知识点描述:创建含有变量的字符串,最终变量被值字符串替换掉。
问题描述:使用变量值替换字符串中的变量,请从以下选项中选出你认为正确的答案:
A.
string = 'The price of {fruit} is ${price}.'
result = string.format()
print(result)
B.
string = 'The price of {fruit} is ${price}.'
result = string.format(fruit = 'apple')
print(result)
C.
string = 'The price of {fruit} is ${price}.'
fruit = 'apple'
price = 2
result = string.format(vars())
print(result)
D.
string = 'The price of {fruit} is ${price}.'
fruit = 'apple'
price = 2
result = string.format_map(vars())
print(result)
正确答案:D
试题代码地址
以上是关于仅需10道题轻松掌握Python字符串方法 | Python技能树征题的主要内容,如果未能解决你的问题,请参考以下文章
仅需10道题轻松掌握Matplotlib图形处理 | Python技能树征题
仅需6道题轻松掌握Python时间和日期处理 | Python技能树征题
仅需6道题轻松掌握SciPy空间计算基础 | Python技能树征题
仅需8道题轻松掌握Python Web应用开发 | Python技能树征题