---
id: 69738771fb5a7b8b24cca29e
title: "Challenge 172: Letters-Numbers"
challengeType: 29
dashedName: challenge-172
---

# --description--

Given a string containing only letters and numbers, return a new string where a hyphen (`-`) is inserted every time the string switches from a letter to a number, or a number to a letter.

# --hints--

`separate_letters_and_numbers("ABC123")` should return `"ABC-123"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(separate_letters_and_numbers("ABC123"), "ABC-123")`)
}})
```

`separate_letters_and_numbers("Route66")` should return `"Route-66"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(separate_letters_and_numbers("Route66"), "Route-66")`)
}})
```

`separate_letters_and_numbers("H3LL0W0RLD")` should return `"H-3-LL-0-W-0-RLD"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(separate_letters_and_numbers("H3LL0W0RLD"), "H-3-LL-0-W-0-RLD")`)
}})
```

`separate_letters_and_numbers("a1b2c3d4")` should return `"a-1-b-2-c-3-d-4"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(separate_letters_and_numbers("a1b2c3d4"), "a-1-b-2-c-3-d-4")`)
}})
```

# --seed--

## --seed-contents--

```py
def separate_letters_and_numbers(s):

    return s
```

# --solutions--

```py
def separate_letters_and_numbers(s):
    if not s:
        return s

    result = s[0]

    for i in range(1, len(s)):
        prev = s[i - 1]
        curr = s[i]

        prev_is_letter = prev.isalpha()
        curr_is_letter = curr.isalpha()

        if prev_is_letter != curr_is_letter:
            result += "-"

        result += curr

    return result
```
