代码如下:
[root@localhost test]# cat 3.py #coding=utf-8 ev = """ 1evilxr 2www 3nihao 4evilxr.com 5dkfjls 6cnblog 7ldskf 8guess 9right """ print ev #打印ev ev1 = ev.replace('\n','').strip() #去掉ev首尾的空格 print ev1 a = ev1.find(" ") #寻找第一个空格的位置 w1 = ev1[:a] #利用切片切出第一个部分 print w1 b = a while b <= len(ev1) and b != -1 :#如果不限制b不为-1的话,就会是死循环 while ev1[a] == " ": a = a + 1 b = ev1.find(" ",a) if b == -1: print ev1[a:] else: print ev1[a:b] a = b
运行结果:
[root@localhost test]# python 3.py 1evilxr 2www 3nihao 4evilxr.com 5dkfjls 6cnblog 7ldskf 8guess 9right 1evilxr 2www 3nihao 4evilxr.com 5dkfjls 6cnblog 7ldskf 8guess 9right 1evilxr 2www 3nihao 4evilxr.com 5dkfjls 6cnblog 7ldskf 8guess 9right