162k views
5 votes
Write a function add_spaces(s) that takes an arbitrary string s as input and uses a loop to form and return the string formed by adding a space between each pair of adjacent characters in the string. You may assume that s is not empty.

User Jhpratt
by
4.8k points

1 Answer

2 votes

Solution:

def add_spaces(s):

result = ""

for i in range(len(s)-1):

result += (s[i]+" ")

result += s[-1]

return result

# Testing

print(add_spaces('hello'))

print(add_spaces('hangman'))

print(add_spaces('x'))

And the output will be

hello

hangman

x

Process finished with exit code 0

User Matthew Savage
by
3.8k points