2024-04-18 03:16:44 发布
网友
如果我有一个str,并且我使用了str.index(char),但是这个字符不存在,那么可以用一行将另一个字符分配给变量吗
str.index(char)
也许像这样
str = "bar/foo" t = str.index("_") | "Empty"
str不包含,因此应该指定字符串“Empty”
str
由于^{}会在子字符串不在字符串中时抛出ValueError,因此将其包装到if/else中,通过in运算符检查字符串中是否存在子字符串:
ValueError
in
>>> s = "bar/foo" >>> s.index("_") if "_" in s else "Empty" "Empty" >>> s = "bar_foo" >>> s.index("_") if "_" in s else "Empty" 3
由于^{} 会在子字符串不在字符串中时抛出
ValueError
,因此将其包装到if/else中,通过in
运算符检查字符串中是否存在子字符串:相关问题 更多 >
编程相关推荐