Reverse a string · 反转字符串
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
This one builds on a lesson you haven't finished yet. Do that lesson first: · 此任务基于您尚未完成的课程。请先完成以下课程:
Python Basics · Python 基础 · Functions · 函数 →
English
Reverse a string
Return the string with its characters back-to-front: reverse_string("abc") gives "cba".
Python slicing has a neat trick: s[::-1] walks the string with a step of -1, from the end to the start. You can also build the answer with a loop if you prefer.
中文
反转字符串
返回字符前后颠倒的字符串:reverse_string("abc") 得到 "cba"。
Python 的切片有个巧妙的技巧:s[::-1] 以 -1 的步长遍历字符串,也就是从末尾走到开头。如果你愿意,也可以用循环来构建答案。
Write reverse_string(s) that returns s with its characters in reverse order. A slice s[::-1] does it in one step. · 编写 reverse_string(s),返回字符顺序颠倒后的 s。切片 s[::-1] 一步即可完成。
Click Run to see the output here. · 点击“运行”查看此处输出。