Py&禅

博客园 首页 新随笔 联系 订阅 管理

2010年5月31日 #

摘要: Problem:You want to reverse the characters or words in a string.Solution:使用字符串的切片操作 [start: stop : step], 当 step为-1时,可实现字符反转操作revchars = astring [::-1]为了实现words的反转,需要建立一个列表来保存words,astring = 'hello wo... 阅读全文
posted @ 2010-05-31 12:43 Py&禅 阅读(246) 评论(0) 推荐(0)

摘要: Problem:将若干个字符串合并到一起Solution:使用 ''.join()来实现对字符串列表的合并。(效率较高)largestring = ''.join(smallpieces)使用%格式符号largeString = '%s%s something %s yet more' % (small1, small2, small3)或使用 reduce()import operatorlar... 阅读全文
posted @ 2010-05-31 07:18 Py&禅 阅读(141) 评论(0) 推荐(0)