输入英文,每一句内容翻转. 标点符号(,.?!)位置不动

# # where are you going?I am going home.
# # 输出处理后的文件,文件内容期望是:
# # going you are where?home going am I.
import string

s = "where are you going!I am going home."
start = 0
end = 0
length = len(s)
# s2 = s.split()
# print("s2:",s2)
result = ""

for i in range(len(s)):
    #print(s[i])
    if s[i] in string.punctuation:
        end = i
        result += " ".join(s[start:end].split()[::-1])
        # s[i]标点符号
        result += s[i]
        start = end + 1

if end != length - 1:
    end = length
    result += " ".join(s[start:end].split()[::-1])

print(result)

  going you are where!home going am I.

原文地址:https://www.cnblogs.com/testerren/p/14054980.html