import re
def reserve(str_list, start, end):
while start <= end:
str_list[start], str_list[end] = str_list[end], str_list[start]
end -= 1
start += 1
str = ' I love china! '
str_list = list(str)
print(str_list)
i = 0
print(len(str_list))
# 从前往后遍历list,如果碰到空格,就调用反转函数,不考虑单个字符情况
while i < len(str_list):
if str_list[i] != ' ':
start = i
end = start + 1
print(end)
while (end < len(str_list)) and (str_list[end]!=' '):
end += 1
if end - start > 1:
reserve(str_list, start, end-1)
i = end
else:
i = end
else:
i += 1
print(str_list)
str_list.reverse()
print(''.join(str_list))
# 采用正则表达式操作
str_re = re.split(r'(\s+)',str)
str_re.reverse()
str_re = ''.join(str_re)
print(str_re) 以上就是Python字符串处理实例详解的详细内容,更多请关注Gxl网其它相关文章!
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did85003