例如,如果我有以下字符串:如何从字符串中提取以特定字母/字符开头的子字符串?
fruits = "The apples are $3.00, the oranges are $2.00, and the grapes are $10000."
我想提取的价格像这样的列表:
['$3.00', '$2.00', '$10000']
到目前为止,我已经做到了这一点:
def extract_prices(s):
prices = []
for i in range(len(s)):
if s[i] == '$':
prices.append(s[i], s.find(' '))
我觉得最后一行是给我的问题。我不知道如何获得价格之后的空间索引,以及如何在该空间停止索引。
任何提示?感谢您阅读本文!对不起,如果我的解释不清楚。
看看正则表达式https://docs.python.org/3 /library/re.html模块 – pythad
对不起,这会有帮助吗?这有点长,我不知道从哪里开始:o谢谢你的回复! – Katerina
正则表达式对于字符串解析非常有用。考虑看看他们的教程 – qxz