efficiently checking that string consists of one character in Python

后端 未结 8 1256
太阳男子
太阳男子 2020-11-29 01:03

What is an efficient way to check that a string s in Python consists of just one character, say \'A\'? Something like all_equal(s, \'A\')

相关标签:
8条回答
  • 2020-11-29 02:06

    Try using the built-in function all:

    all(c == 'A' for c in s)
    
    0 讨论(0)
  • 2020-11-29 02:08

    Adding another solution to this problem

    >>> not "AAAAAA".translate(None,"A")
    True
    
    0 讨论(0)
提交回复
热议问题