在Python中,美元通常使用`$`符号来表示。例如,如果你想在字符串中表示美元金额,你可以这样做:
price_in_usd = "$123.45"
如果你需要在字符串中查找以美元符号开头的金额,你可以使用正则表达式。例如,使用`re`模块来查找所有以美元符号开头的数字:
import re
text = "The price is $123.45 and the discount is $20.00."
pattern = r'\$\d+(\.\d{2})?'
matches = re.findall(pattern, text)
for match in matches:
print(f"Found a price: {match}")
输出将是:
Found a price: $123.45
Found a price: $20.00
请注意,在正则表达式中使用`$`符号时,如果你想要匹配实际的美元符号字符,你需要使用`\`对其进行转义,像这样:`r'\$\d+(\.\d{2})?'`。如果你只是想要匹配字符串末尾的美元符号,你不需要转义,因为它不是正则表达式的一部分,而是作为普通字符出现的。