pythonCookbook2学习笔记---第一章引言
print 'this is literal string'#单引号
print "this is anthor"#双引号
print 'isn\'t'#遇到一样的单引号要转义
print "isn't"
big1 = "this is literal \
string"#字符串过长时如何打印成一行
print big1
big2 ="this is literal\n\
string"#字符串打印成两行
print big2
bigger = """
this
is
an
even
"""
print bigger#字符串打印成多行
big3=r"this is a long string\
with a backslash"
print big3#"原"字符串
hello = u'hello\u0020world'
print hello#\u0020 Unicode字符的空格
mystr ="my string"
print mystr
print mystr[0]#通过索引访问单个字符
print mystr[1:3]#切片
print mystr[3:]#切片
print mystr[-3:]#切片
print mystr[:3:1]#加步长
print mystr[:3:-1]
print mystr[::2]
print mystr[::-2]
for c in mystr:#循环遍历字符串
list(mystr)
print list(mystr)
print mystr+'old'
print mystr*3
print mystr+' '+big1
s='1234'
print s.isdigit()#测试字符串是不是都是数字
print mystr.isdigit()
print mystr.upper()#转换成大写
print mystr.count('m')#在mystr中搜索m
print big2.count('s')
one_large_string = """
[2018-03-26 18:54:00 ]service HsSelfCheckSrv not run, try run it but failed.
[2018-03-26 18:54:05 ]hooker function failed.
[2018-03-26 18:54:21 ]service hsnet not run, try run it but failed.
[2018-03-26 18:54:21 ]service hsefs not run, try run it but failed.
[2018-03-26 18:54:21 ]service HsSelfCheckSrv not run, try run it but failed.
[2018-03-26 18:54:26 ]hooker function failed.
[2018-03-26 18:54:36 ]service hsnet not run, try run it but failed.
[2018-03-26 18:54:36 ]service hsefs not run, try run it but failed.
[2018-03-26 18:54:36 ]service HsSelfCheckSrv not run, try run it but failed.
[2018-03-27 08:43:21 ]------------Start---------------
"""
list_of_lines = one_large_string.splitlines()#大段文字分行
print list_of_lines
one_large_string_new='\n'.join(list_of_lines)#换行后合成新的字符串
print one_large_string_new

浙公网安备 33010602011771号