2014-09-25 134 views
0

在python中是否有一個函數來檢查一個字符串是否以一定數量的可能性開始? 例如,我想檢查字符串A是以「Ha」,「Ho」,「Hi」開頭的。我以爲我可以使用string_A.startswith("Ha", "Ho", "Hi"),但不幸的是,這是不可能的:(Startswith():更多參數

感謝您的任何意見:)

+2

[通過他們在一個元組中。](https://docs.python.org/2/library/stdtypes.html#str.startswith) – 2014-09-25 13:51:20

回答

2

你需要將它們傳遞作爲一個元組:

>>> "Hi, there".startswith(('Ha', 'Ho', 'Hi')) 
True 
>>> "Hoy, there".startswith(('Ha', 'Ho', 'Hi')) 
True 
>>> "Hello, there".startswith(('Ha', 'Ho', 'Hi')) 
False