How good is startswith?

前端 未结 10 444
执笔经年
执笔经年 2021-01-07 19:38

Is

text.startswith(\'a\')  

better than

text[0]==\'a\'  

?

Knowing text is not empty and we a

10条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 19:58

    Personally I would say startswith is more readable.

    Also, from Python 2.5 startwith can take a tuple of prefixes to look for:

    >>> "hello world".startswith(("hello","goodbye"))
    True
    

提交回复
热议问题