Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding spaces between characters inside a string

Tags:

python

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)
like image 617
coding girl Avatar asked Aug 04 '26 07:08

coding girl


1 Answers

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)
like image 168
blhsing Avatar answered Aug 06 '26 21:08

blhsing