我想知道如何剥离所有整数和空格的输入。我知道.strip()
Python中的函数可以做到这一点,但它仅适用于字符串开头/结尾的字符。
这是我的代码:
battery = input("Is the phone charger turned on at the plug?").lower()
if battery == "y" or battery == "yes":
print("Replace the phone's battery or contact the phone's manufacturer.")
break
因此,如果用户输入“ ye2s”,则程序将摆脱“ 2”,并将其视为“是”。
您可以使用isdigit()
字符串方法按如下方式进行操作:
battery = ''.join(c for c in battery if not c.isdigit() and not c.isspace())
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句