def reverse_string_by_word(s): lst = s.split() # split by blank space by default return ' '.join(lst[::-1])s = 'power of love'print reverse_string_by_word(s)# love of powers = 'hello world!'print reverse_string_by_word(s)# world! hello
上面的实现其实已经能满足大多数情况,但是并不完美。比如第二个字符串中的感叹号并没有被翻转,而且原字符串中的空格数量也没有保留。(在上面的例子里其实hello和world之间不止一个空格)
我们期望的结果应该是这样子的。
print reverse_string_by_word(s)# expected: !world hello
要改进上面的方案还不把问题复杂化,推荐使用re模块。你可以查阅re.split() 的官方文档。我们看一下具体例子。
>>> import re>>> s = 'hello world!'>>> re.split(r'\s+', s) # will discard blank spaces['hello', 'world!']>>> re.split(r'(\s+)', s) # will keep spaces as a group['hello', ' ', 'world!']>>> s = ''>>> re.split(r'\s+', s) # split by spaces['']>>> re.split(r'(\w+)', s) # exactly split by word['']>>> re.split(r'(\s+|\w+)', s) # split by space and word['']>>> ''.join(re.split(r'(\s+|\w+)', s)[::-1])'> !com.ef to welcome >> ''.join(re.split(r'(\s+)', s)[::-1])'> ef.com! to welcome >> ''.join(re.split(r'(\w+)', s)[::-1])'! >com.ef to welcome>> ''.join(reversed(re.split(r'(\s+|\w+)', s)))'> !com.ef to welcome <'
一句话搞定,so easy!
python翻转字符串(reverse string), 一共包含5种方法, 其中第一种最简单, 即步长为-1, 输出字符串;
方法如下
5种方法的比较:
1. 简单的步长为-1, 即字符串的翻转(常用);
2. 交换前后字母的位置;
3. 递归的方式, 每次输出一个字符;
4. 双端队列, 使用extendleft()函数;
5. 使用for循环, 从左至右输出;
代码:
# -*- coding: utf-8 -*- #eclipse pydev, python 3.3 #by c.l.wang #time: 2014. 4. 11 string = 'abcdef' def string_reverse1(string): return string[::-1] def string_reverse2(string): t = list(string) l = len(t) for i,j in zip(range(l-1, 0, -1), range(l//2)): t[i], t[j] = t[j], t[i] return .join(t) def string_reverse3(string): if len(string) <= 1: return string return string_reverse3(string[1:]) + string[0] from collections import deque def string_reverse4(string): d = deque() d.extendleft(string) return ''.join(d) def string_reverse5(string): #return ''.join(string[len(string) - i] for i in range(1, len(string)+1)) return ''.join(string[i] for i in range(len(string)-1, -1, -1)) print(string_reverse1(string)) print(string_reverse2(string)) print(string_reverse3(string)) print(string_reverse4(string)) print(string_reverse5(string))
输出:
fedcba fedcba fedcba fedcba fedcba
