Write a function that returns a string with its characters in reverse order.
The report names this warm-up only as reversing a string. This version fixes the input to printable ASCII, so that reversing characters and reversing bytes give the same result, and asks for the reversed string as the return value.
Function Signature
def reverse_string(s: str) -> str:
Rules
-
Return a string of the same length whose character at index
i
is
s[len(s) - 1 - i]
.
-
Every character moves, including spaces and punctuation; nothing is trimmed, collapsed or changed in case.
-
An empty input returns an empty string.
Constraints
-
0 <= len(s) <= 10^5
-
s
contains only printable ASCII characters (codes 32 to 126).
Examples
Example 1
Input: s = "firmware"
Output: "erawmrif"
Example 2
Input: s = "a b!"
Output: "!b a"
The space and the exclamation mark move like any other character.
Example 3
Input: s = ""
Output: ""