python中是否有一个函数来检查字符串是否以一定数量的可能性开头?例如,我想检查字符串A是否以"Ha“、"Ho”、"Hi“开头。我以为我可以使用string_A.startswith("Ha", "Ho", "Hi"),但不幸的是,这是不可能的:
(谢谢你的建议!)
发布于 2014-09-25 13:51:50
您需要将它们作为一个元组传递:
>>> "Hi, there".startswith(('Ha', 'Ho', 'Hi'))
True
>>> "Hoy, there".startswith(('Ha', 'Ho', 'Hi'))
True
>>> "Hello, there".startswith(('Ha', 'Ho', 'Hi'))
Falsehttps://stackoverflow.com/questions/26040346
复制相似问题