Solution 1

def insert_dashes(a_string):
    counter = 1
    res = ""
    while counter <= len(a_string):
        char = a_string[counter - 1]
        res += char
        if counter % 3 == 0:
            res += '-'
        counter += 1
    return res

Solution 2

def insert_dashes(a_string):
    counter = 1
    res = ""
    for char in a_string:
        res += char
        if counter == 3:
            res += '-'
            counter = 1
        else:
            counter += 1

    return res

Insert dashes every 3 characters

Write a function insert_dashes that receives a string and inserts dashes every 3 characters. Example:

insert_dashes('Hello World')  # 'Hel-lo -Wor-ld'
insert_dashes('AwesomePythonRmotr')  # 'Awe-som-ePy-tho-nRm-otr-'

Test Cases

test with large string - Run Test

def test_with_large_string():
    assert insert_dashes('Python is Awesome') == 'Pyt-hon- is- Aw-eso-me'

test with short string - Run Test

def test_with_short_string():
    assert insert_dashes('Python') == 'Pyt-hon-'
def insert_dashes(a_string): pass