Dataford
Interview Guides
Upgrade
All questions/Coding/Reverse Characters in a String

Reverse Characters in a String

Easy
Coding
ArraysStringsRecursion

Problem

Problem

At Apple, a text-processing utility needs a simple helper to reverse user-entered strings. Write a function that takes a string and returns a new string with the characters in reverse order.

Formal Specification

  • Input: a string s
  • Output: a string containing the same characters as s, but in reverse order

Your solution should work for empty strings, strings with spaces, punctuation, digits, and mixed case characters.

Examples

Example 1

Input: s = "hello"
Output: "olleh"

Explanation: The characters are returned from last to first.

Example 2

Input: s = "Swift 5"
Output: "5 tfiwS"

Explanation: Spaces and digits are treated like any other character and are also reversed.

Example 3

Input: s = ""
Output: ""

Explanation: Reversing an empty string still produces an empty string.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain letters, digits, spaces, and punctuation
  • Return a new reversed string

Examples

Example 1
Inputs = "hello"Output"olleh"WhyThe characters are reversed from `h e l l o` to `o l l e h`.
Example 2
Inputs = "Swift 5"Output"5 tfiwS"WhyThe digit and space are reversed along with the letters.
Example 3
Inputs = "a"Output"a"WhyA single-character string is unchanged when reversed.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain letters, digits, spaces, and punctuation
  • Return a new reversed string

Function Signature

def reverse_string(s):

Problem

Problem

At Apple, a text-processing utility needs a simple helper to reverse user-entered strings. Write a function that takes a string and returns a new string with the characters in reverse order.

Formal Specification

  • Input: a string s
  • Output: a string containing the same characters as s, but in reverse order

Your solution should work for empty strings, strings with spaces, punctuation, digits, and mixed case characters.

Examples

Example 1

Input: s = "hello"
Output: "olleh"

Explanation: The characters are returned from last to first.

Example 2

Input: s = "Swift 5"
Output: "5 tfiwS"

Explanation: Spaces and digits are treated like any other character and are also reversed.

Example 3

Input: s = ""
Output: ""

Explanation: Reversing an empty string still produces an empty string.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain letters, digits, spaces, and punctuation
  • Return a new reversed string

Examples

Example 1
Inputs = "hello"Output"olleh"WhyThe characters are reversed from `h e l l o` to `o l l e h`.
Example 2
Inputs = "Swift 5"Output"5 tfiwS"WhyThe digit and space are reversed along with the letters.
Example 3
Inputs = "a"Output"a"WhyA single-character string is unchanged when reversed.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain letters, digits, spaces, and punctuation
  • Return a new reversed string

Function Signature

def reverse_string(s):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
SkillshareReverse Characters in a StringEasyPaycomReverse Characters in a StringEasyEverlight SolarReverse Characters in a StringEasy
Next question