50 XP

54. Reverse only the letters

Intermediate two pointers strings

Write a function reverse_letters(text) that takes a string and returns it with its letters in reverse order, while every character that isn't a letter (spaces, digits, punctuation) stays at its position.

In other words, the positions that hold letters stay letter positions, and they are filled with the same letters, last to first. Capitals stay capitals: H moves as H.

Example 1

Inputtext = "Hi, you!"
Output"uo, yiH!"
ExplanationLetters reversed; comma, space and ! stay.

Example 2

Inputtext = "a1b2c3"
Output"c1b2a3"
ExplanationThe digits keep their places.

Example 3

Inputtext = "12345"
Output"12345"
ExplanationNo letters to move.

Constraints

  • text has 0 to 100 characters: English letters, digits, spaces and punctuation.

Submit also runs 9 more tests you can't see.

Practises String Methods (lesson 5).

Code
def reverse_letters(text):
    """Reverse the letters, leaving everything else put.

    text: str; may be empty
    Returns: str, the same length, letters reversed and
        every other character in its place
    """
    # Write your code here
    pass