python - 打印子字符串,Python

标签 python string substring

我正在编写一个函数,用于返回用户给出的 firstWord 和 secondWord 之间的字符串中的单词。

def returnWordsBetween(firstWord, secondWord, inputString):
    start = inputString.find(firstWord)
    end = inputString.find(secondWord)
    inputString =(start, end) 
    print inputString 

    firstWord = '<data>'
    secondWord = '</data>'
    inputString = 'fooz bizz <data> 31.25 funnel </data> fuzz'

这看起来有用吗?我没有打印任何东西,也没有错误代码,不确定发生了什么

最佳答案

你可以使用正则表达式。

print re.search(firstWord+r'(.*?)'+secondWord, inputString).group(1)

关于python - 打印子字符串,Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715953/

相关文章:

Python-给定输入字母的可能的英语单字变位词

python - Cython 将字符串转换为 unicode

Python正则表达式匹配完整或部分单词

python - PyQt5 QWebEngine 禁用 Access-Control-Allow-Origin

python - 计算某些文本中多字子串的出现次数

python - 简单的python/Beautiful Soup类型问题

java - 使用变量名读取变量值

javascript - 在数组中查找子字符串 - javascript

json - 基于字符串数组在 jq 中进行过滤

python ImportError Openvino 通过脚本和 shell