(9.1)Python测试之记录
- How can you print a string without resolving escape sequences in Python?
A. By using the repr() function
B. By using the eval() function
C. By using the str() function
D. By using the format() function
中文翻译:
在 Python 中,如何打印字符串而不解析转义序列?
A. 使用 repr() 函数
B. 使用 eval() 函数
C. 使用 str() 函数
D. 使用 format() 函数
正确答案:A
- 分析:
- Python中 repr() 函数确保转移序列显示为字符串的一部分而不是被处理。
a = "Hello\nWorld\t!"
print(repr(a))
- eval() 函数用来执行一个字符串表达式,并返回表达式的值。
字符串表达式可以包含变量、函数调用、运算符和其他 Python 语法元素.
print(eval("2 * 2"))
a = 7
print(eval("a + 3"))
- str() 函数将对象转化为适于人阅读的形式。
a = [1, 2, 3]
print(str(a))
- format() 格式化字符串
print("教室:{classroom}, 教师:{teacher}".format(classroom=204, teacher="Bat"))
- What is the time complexity of the find() function in Python?
A. O(log n)
B. O(n^2)
C. O(n)
D. O(1)
中文翻译:
Python 中 find() 函数的时间复杂度是多少?
A. O(log n)
B. O(n^2)
C. O(n)
D. O(1)
正确答案:C
- 分析
For a string of length n and a substring of length m, the find() method compares each possible starting position in the string where the substring could fit, and checks for a match. If no match is found, it will continue until the end of the string.
Therefore the time complexity for find() in O(n) where n is the length of the string.
对于长度为 的字符串n和长度为 的子字符串m,该 find() 方法会比较字符串中每个可能的起始位置,以找到子字符串可以容纳的位置,并检查是否匹配。如果没有找到匹配项,则继续执行,直到字符串末尾。
因此 find() 的时间复杂度为 O(n),其中 n 是字符串的长度。
- What does the isalpha() function in Python return?
A. True if all characters in the string are alphabets, else False.
B. True if all characters in the string are numbers, else False.
C. True if all characters in the string are alphanumeric, else False.
D. True if all characters in the string are spaces, else False.
中文翻译:
Python 中的 isalpha() 函数返回什么?
A. 如果字符串中的所有字符都是字母,则为 True,否则为 False。
B. 如果字符串中的所有字符都是数字,则为 True,否则为 False。
C. 如果字符串中的所有字符都是字母数字,则为 True,否则为 False。
D. 如果字符串中的所有字符都是空格,则为 True,否则为 False。
正确答案: A
- 分析
In Python, the isalpha() method is used to check whether all characters in a string are alphabetic (i.e., they are letters).
The method returns True if all characters in the string are alphabetic and the string is non-empty, otherwise, it returns False.
在 Python 中,该 isalpha() 方法用于检查字符串中所有字符是否均为字母(即,它们都是字母)。如果字符串中的所有字符均为字母且字符串非空,则该方法返回;否则,返回。True False
a = "hello23"
print(a.isalpha())
a = "hello"
print(a.isalpha())
- Which method is best suited to replace all tab characters with whitespace using the given tab size?
A. strip()
B. expandtabs()
C. replace()
D. maketrans()
中文翻译:
哪种方法最适合使用给定的制表符大小将所有制表符替换为空格?
A. strip()
B. expandtabs()
C. replace()
D. maketrans()
正确答案:B
- 分析:
expandtabs() 方法把字符串中的 tab 符号 \t 转为空格,tab 符号 \t 默认的空格数是 8,在第 0、8、16…等处给出制表符位置,如果当前位置到开始位置或上一个制表符位置的字符数不足 8 的倍数则以空格代替。
- Which method is used to create a Regex object in Python?
A. regex.compile()
B. re.create()
C. re.compile()
D. regex.create()
中文翻译:
在 Python 中,哪种方法用于创建 Regex 对象?
A. regex.compile()
B. re.create()
C. re.compile()
D. regex.create()
正确答案:C
- 分析:
re.compile() function compiles a regular expression pattern into a Regex object, which can be used for matching and searching efficiently.
re.compile() 函数将正则表达式模式编译为 Regex 对象,可用于高效地匹配和搜索。
- What is the purpose of the translate() function in Python?
A. It is used to map the contents of string 1 with string 2 with respective indices to be translated later.
B. It is used to swap the string elements mapped with the help of maketrans().
C. It is used to replace the substring with a new substring in the string.
D. It is used to replace all tab characters with whitespace using the given tab size.
中文翻译:
Python 中的 transform() 函数的用途是什么?
A. 它用于将字符串 1 的内容与字符串 2 进行映射,并分别使用相应的索引进行稍后的翻译。
B. 它用于在 maketrans() 的帮助下交换映射的字符串元素。
C. 它用于用字符串中的新子字符串替换子字符串。
D. 它用于使用给定的制表符大小将所有制表符替换为空格。
正确答案:B
- 分析:
translate() function modifies a string based on a translation table created using maketrans().
translation() 函数根据使用 maketrans() 创建的翻译表修改字符串。【translate() 方法根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。】
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)str = "this is string example....wow!!!"
print(str.translate(trantab))