Solution 1

def get_string_positions(the_string):
    result = ""
    for pos, char in enumerate(the_string):
        result += str(pos) + '-' + char + '\n'
    return result

String Positions

Sometimes you need to know the position of a character in a string and you might not want to count to figure it out. So write a quick function to do it for you!

Complete get_string_positions so that it takes the_string and creates a new string showing the position of each character. Example:



The function uses the enumerate() function on the list in a for loop to get each position and character associated with it. Example:

for position, character in enumerate(list_name):
    # do stuff

Then just store the result in a big string. Note you separate the lines by using '\n' the newline character after each line. Good luck!

Hint: You might have to put str() around to position to change it from an integer into a string so you can add it to your result string.

Test Cases

test string positions - Run Test

def test_string_positions():
    assert get_string_positions("abcd") == "0-a\n1-b\n2-c\n3-d\n"
def get_string_positions(the_string): pass