Python Checking a string's first and last character

后端 未结 4 1431
南方客
南方客 2020-12-24 10:33

can anyone please explain what is wrong with this code?

str1=\'\"xxx\"\'
print str1
if str1[:1].startswith(\'\"\'):
    if str1[:-1].endswith(\'\"\'):
               


        
4条回答
  •  情书的邮戳
    2020-12-24 10:56

    You should either use

    if str1[0] == '"' and str1[-1] == '"'
    

    or

    if str1.startswith('"') and str1.endswith('"')
    

    but not slice and check startswith/endswith together, otherwise you'll slice off what you're looking for...

提交回复
热议问题