MST
星途 面试题库

面试题:Python中findall和finditer查找位置的基础应用

给定字符串 'hello world, hello python',使用Python的re模块中的findall和finditer方法,分别找出所有 'hello' 出现的位置,并以列表形式输出。要求写出完整代码。
16.0万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
import re

s = 'hello world, hello python'
# 使用findall方法
result_findall = []
for match in re.finditer('hello', s):
    result_findall.append(match.start())
print("findall方法结果:", result_findall)

# 使用finditer方法
result_finditer = []
for match in re.finditer('hello', s):
    result_finditer.append(match.start())
print("finditer方法结果:", result_finditer)