How good is startswith?

前端 未结 10 508
执笔经年
执笔经年 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:52

    text[0] can fail but the equivalent text[:1] is safe if the string is empty.

    If you want to compare more than one characters, I believe .startswith() is better.

提交回复
热议问题