python学习笔记之反向输出英文句子

1.编写reverse_sentence()函数体

def reverse_sentence(s):
    s = s.split()
    s = " ".join(s[::-1])
    return s

s表示接收到的字符串,即输入的句子。

s.split()对字符串进行切割形成列表,将列表反向并以空格连接成字符串。

返回s

2.引导用户输入一个句子

sentence = input("Please input a sentence: ")

3.调用 reverse_sentence()函数,打印结果

y = reverse_sentence(sentence)
print(y)

完整代码如下:

sentence = input("Please input a sentence: ")

def reverse_sentence(s):
    s = s.split()
    s = " ".join(s[::-1])
    return s

y = reverse_sentence(sentence)
print(y)

http://www.runoob.com/python3/python3-string.html

 

你可能感兴趣的:(python)