如何去除字符串忽略大小写?

Jef*_*ort -1 python

我试图从error变量中去除“错误:”这个词,我想去除错误:对于所有情况?我该怎么做?

error = "ERROR:device not found"
#error = "error:device not found"
#error = "Error:device not found"
print error.strip('error:')
Run Code Online (Sandbox Code Playgroud)

cwa*_*ole 7

re模块可能是您最好的选择:

re.sub('^error:', '', 'ERROR: device not found', flags=re.IGNORECASE)

# '^error:' means the start of the string is error:
# '' means replace it with nothing
# 'ERROR: device not found' is your error string
# flags=re.IGNORECASE means this is a case insensitive search.

# In your case it would probably be this:
re.sub('^error:', '', error, flags=re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)

这将删除ERROR:字符串开头的所有变体。