Python案例:倒置英文句子

Python案例:倒置英文句子

任务:给你一个英语句子,比如"London bridge is falling down",把它完全倒装过来,"down falling is bridge London"。

一、编写源代码


"""
给你一个英语句子,比如"London bridge is falling down",
把它完全倒装过来,成为"down falling is bridge London"。
"""

sentence = "London bridge is falling down"
print("原始语句:" + sentence)

print("\n方法一:句子拆分-单词数组-数组倒序")
words = sentence.split(" ")
count = len(words)
for i in range(count):
    print(words[i], end=" ")
print()
for i in range(count):
    print(words[count - i - 1], end=" ")

print("\n\n方法二:句子倒序-拆分句子-单词倒序")
sentence = sentence[::-1]
print(sentence)
words = sentence.split(" ")
count = len(words)
for i in range(count):
    print(words[i][::-1], end=" ")

二、查看运行效果


猜你喜欢

转载自blog.csdn.net/howard2005/article/details/80781621