2013年5月15日
摘要: Python中的splitlines用来分割行。当传入的参数为True时,表示保留换行符 \n。通过下面的例子就很明白了:mulLine="""Hello!!!WellcometoPython'sworld!Therearealotofinterestingthings!Enjoyyourself.Thankyou!"""print''.join(mulLine.splitlines())print'------------'print''.join(mulLine.splitl 阅读全文
posted @ 2013-05-15 13:41 一个人的天空@ 阅读(224) 评论(0) 推荐(0) 编辑
摘要: Python中按一定的格式取出某字符串中的子字符串,使用struck.unpack是非常高效的。1. 设置fomat格式,如下:#取前5个字符,跳过4个字符,再取3个字符format='5s4x3s'2. 使用struck.unpack获取子字符串importstruct printstruct.unpack(format,'Testastring')#('Test','ing')来个简单的例子吧,有一个字符串'He is not very happy',处理一下,把中间的not去掉,然后再输出。importstr 阅读全文
posted @ 2013-05-15 12:17 一个人的天空@ 阅读(330) 评论(0) 推荐(0) 编辑
摘要: 开始以为Python中没有像其他语言一样的条件判断的缩写形式:return(1==1)?"is easy":"my god"//C#中的用法其实,在Python中,是这样写的:print(1==2)and'Fool'or'Notbad'输出结果:Not bad转帖注:其实python支持原生的条件判断缩写,如下:print 'Fool' if 1 == 2 else 'Not bad' 阅读全文
posted @ 2013-05-15 12:12 一个人的天空@ 阅读(527) 评论(0) 推荐(0) 编辑
摘要: 转换大小写和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower()。还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及所有单词首字母大写,其余小写的title()方法。函数较简单,看下面的例子:s='hEllopYthon'prints.upper()prints.lower()prints.capitalize()prints.title()输出结果:HELLO PYTHONhello pythonHello pythonHello Python判断大小写Python提供了isuppe 阅读全文
posted @ 2013-05-15 12:07 一个人的天空@ 阅读(338) 评论(0) 推荐(0) 编辑