字符串的比较:
1、=
2、startswitch()
3、endswitch()
字符串反转:
1、=
2、startswitch()
3、endswitch()
print'--------------------------'
a = 1
b = '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==b
a!=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,datetime
print time.strftime('%Y-%m-%d %x %X')
t = time.strptime('08/29/16 16:29:30','%x %X')
print t
import re
str = r'^"1"'
print re.findall(r'^[a-z]\w*\@[a-z]+\.[com]{3}','s107a880@qq.com')<pre name="code" class="python">输出12
3
12
123xxc34,djfxxf
2016-08-29 08/29/16 18:01:56
time.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']
本文介绍了Python中字符串的基础操作方法,包括字符串的比较、反转及查找等实用技巧,并通过具体示例展示了如何使用这些方法来处理实际问题。

被折叠的 条评论
为什么被折叠?



