当前位置: 首页 > news >正文

(9.1)Python测试之记录

  1. 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"))

在这里插入图片描述

  1. 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 是字符串的长度。
  1. 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())

在这里插入图片描述

  1. 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 的倍数则以空格代替。
  1. 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 对象,可用于高效地匹配和搜索。
  1. 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))

在这里插入图片描述

http://www.xdnf.cn/news/1416133.html

相关文章:

  • Shell 编程 —— 正则表达式与文本处理器
  • 函数,数组与正则表达式
  • Android原生HttpURLConnection上传图片方案
  • 打造智能写作工作流:n8n + 蓝耘MaaS平台完整实战指南
  • Apollo学习之决策模块
  • 【Linux手册】Unix/Linux 信号:原理、触发与响应机制实战
  • Ajax笔记(下)
  • 在.NET标准库中进行数据验证的方法
  • Java视觉跟踪入门:使用OpenCV实现实时对象追踪
  • 【开题答辩全过程】以 基于php的校园兼职求职网站为例,包含答辩的问题和答案
  • 【Android】使用Handler做多个线程之间的通信
  • 【Flask】测试平台开发,应用管理模块实现-第十一篇
  • 【lucene核心】impacts的由来
  • 旧物回收小程序:科技赋能,开启旧物新生之旅
  • 山东省信息技术应用创新开展进程(一)
  • 《C++进阶之STL》【红黑树】
  • OS+MySQL+(其他)八股小记
  • 【macOS】垃圾箱中文件无法清理的常规方法
  • 应用平台更新:可定制目录、基于Git的密钥管理与K8s项目自动化管理
  • Qt中的信号与槽机制的主要优点
  • LeetCode 142. 环形链表 II - 最优雅解法详解
  • 阿里云代理商:轻量应用服务是什么?怎么用轻量应用服务器搭建个人博客?
  • Linux性能调试工具之ftrace
  • JSP 输出语法全面解析
  • 制造业生产线连贯性动作识别系统开发
  • MCP SDK 学习二
  • 【开题答辩全过程】以 基于Java的网络购物平台设计与实现为例,包含答辩的问题和答案
  • 集合-单列集合(Collection)
  • Docker中使用Compose配置现有网络
  • Ubuntu 中复制粘贴快捷键