coding girl
coding girl

Reputation: 183

Adding spaces between characters inside a string

This is a Kata challenge. The function should return a string with spaces between each character. So "Hi there" should equal the the string with spaces between each letter, two spaces between the words. My code actually works in my Python environment, but it is not accepted on Kata.

def spacing(string):
    return " ".join(a for a in string).split(string)

Upvotes: 3

Views: 1320

Answers (2)

kuco 23
kuco 23

Reputation: 830

try

def spacing(string):
   return " ".join(string.replace(" ", ""))

this will work if the only whitespaces in string are spaces.

Upvotes: 0

blhsing
blhsing

Reputation: 106523

A string when iterated over is considered a sequence of characters, so you can simply pass the string to the join method directly:

def spacing(string):
    return ' '.join(string)

Upvotes: 4

Related Questions