当前位置: 首页 > 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.dtcms.com/a/361071.html

相关文章:

  • 面试 TOP101 动态规划专题题解汇总Java版(BM62 —— BM82)
  • 【数学建模学习笔记】数据标准化
  • NUC029芯片解密和产品应用介绍
  • 玻璃门轻松更换智能密码锁,对接会议预约小程序的方法
  • 美术馆预约小程序|基于微信小程序的美术馆预约平台设计与实现(源码+数据库+文档)
  • Jenkins大总结 20250901
  • 今日分享:C++ -- vector
  • Linux 进程状态 — 僵尸进程
  • keil MDK如何使用第三方软件Keil2Json.exe生成compile_commands.json文件,方便vscode+clangd环境使用
  • Java面试-微服务(业务问题)
  • C#在物联网GPS经纬度转换为百度地图地址
  • 再见 K8s!3款开源的云原生部署工具
  • NetCoreKevin-DDD-微服务-WebApi-AI智能体、AISK集成、MCP协议服务、SignalR、Quartz 框架-15-认证与安全
  • DevExpress WinForms中文教程:Data Grid - 过滤编辑器
  • Spring事务管理策略对比与性能优化实践指南
  • k8s--etcd
  • CTFshow系列——命令执行web73-77(完结篇)
  • LeetCode Hot 100 Python (41~50)
  • .NET 微服务日志系统:Serilog + Loki + Grafana 实践指南
  • 安卓11 12系统修改定制化_____常用的几种修改固件 实现指定 “运行内存” 显示
  • 【论文精读】基于YOLOv3算法的高速公路火灾检测
  • ios 配置了代理且使用 chls.pro/ssl 下载不了证书,无法弹出下载证书的提示问题
  • 高防IP防护效果评估全攻略:从指标解读到实战测试
  • python填充多边形,获取所有内部点
  • JVM:内存区域划分、类加载的过程、垃圾回收机制
  • 电影票api接口对接步骤
  • Minecraft(我的世界)服务器信息查询免费API接口详解
  • Java PDF转多种图片格式:技术实践与性能优化
  • Flutter 本地持久化存储:Hive 与 SharedPreferences 实战对比
  • [吾爱出品] PDF文件加密解密工作,附带源码。