小编Chu*_*uvi的帖子

Python检查字符串的第一个和最后一个字符

任何人都可以解释这个代码有什么问题吗?

str1='"xxx"'
print str1
if str1[:1].startswith('"'):
    if str1[:-1].endswith('"'):
        print "hi"
    else:
        print "condition fails"
else:
    print "bye"   
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

Condition fails
Run Code Online (Sandbox Code Playgroud)

但我希望它能打印出来hi.

python string python-2.7

63
推荐指数
3
解决办法
22万
查看次数

如何检查字符串是否有特殊字符?

我只能在我的程序中使用一个字符串,如果它不包含除下划线之外的特殊字符_.我怎么检查这个?

我尝试使用unicodedata库.但是特殊字符刚被标准字符所取代.

python string

23
推荐指数
2
解决办法
11万
查看次数

在python中如何检查字符串是否以特殊符号开头?

我wana检查我的字符串是否以花括号开头{.我尝试了以下代码.

class parser(object):
    def __init__(self):
        self.fp=open('jsondata.txt','r')
        self.str=self.fp.read()
        print "Name of the file Opened is :",self.fp.name 
        print "Contents of the file :\n",self.str
    def rule1(self):
        var='{'
        if self.str[:0]==var:
            print "good match"
        else:
            print "No match"
obj=parser()
obj.rule1()   
Run Code Online (Sandbox Code Playgroud)

该文件包含:{"name":"Chuvi"}
但我的输出是:不匹配

我甚至试过以下但是得到了输出

            if self.str[:0]=='{':
                print "good match"
            else:
                print "No match"
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
3115
查看次数

标签 统计

python ×3

string ×2

python-2.7 ×1