Python字符串2

来源:互联网 发布:nginx 限制访问目录 编辑:程序博客网 时间:2024/05/22 18:21
字符串的比较:
1、=
2、startswitch()
3、endswitch()


print'--------------------------'a = 1b = '1'if a == b:        print '='else:    print '!='print'--------------------------'    a= 'hello word'b= 'hello'if(a.startswith(b,0,6)):    print 'a==b'else:    print 'a!=b'        if(a.endswith('word',7,10)):    print 'a==b'else:    print 'a!=b'输出:--------------------------!=--------------------------a==ba!=b




字符串反转:
def reverse(str):    l = list(str)    out = ''    for i in range(len(l),0,-1):        out += ''+l[i-1]    return out     print reverse('hello')输出:olleh




字符串查找:

正则表达式:

str = '123abc34,djfabf'print str.find('ab',5)print str.find('ab')print str.rfind('ab')print str.replace('ab', 'xx')#print str.replaceFirst('[0-9]{3}',"...")import time,datetimeprint time.strftime('%Y-%m-%d %x %X')t = time.strptime('08/29/16 16:29:30','%x %X') print t import restr = r'^"1"'print re.findall(r'^[a-z]\w*\@[a-z]+\.[com]{3}','s107a880@qq.com')<pre name="code" class="python">输出
12312123xxc34,djfxxf2016-08-29 08/29/16 18:01:56time.struct_time(tm_year=2016, tm_mon=8, tm_mday=29, tm_hour=16, tm_min=29, tm_sec=30, tm_wday=0, tm_yday=242, tm_isdst=-1)['s107a880@qq.com']



0 0