Skip to content

Reverse a string · ⁨反转字符串⁩

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 的步长遍历字符串,也就是从末尾走到开头。如果你愿意,也可以用循环来构建答案。

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩