San*_*nan 2 python case-insensitive
我需要匹配以下字符串File system full。问题是起始 F 可以是小写或大写。当字符串比较通常区分大小写时,如何在 Python 中执行此操作?
我将提供布尔指示器供您使用(if为了简洁起见,而不是实际的块)。
使用正则表达式:
import re
bool(re.match('[F|f]',<your string>)) #if it matched, then it's true. Else, false.
Run Code Online (Sandbox Code Playgroud)
如果字符串可以在输出中的任何位置(我假设是字符串)
import re
bool(re.search('[F|f]ile system full',<your string>))
Run Code Online (Sandbox Code Playgroud)
其他选项:
检查“f”和“F”
<your string>[0] in ('f','F')
<your string>.startswith('f') or <your string>.startswith('F')
Run Code Online (Sandbox Code Playgroud)
还有之前建议的lower方法:
<your string>.lower() == 'f'
Run Code Online (Sandbox Code Playgroud)