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 Power s = '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)
输出:
fedcba fedcba fedcba fedcba fedcba
查看更多关于简单谈谈Python中的反转字符串问题的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did86527