I didn't look at your code in detail, but there is a much easier way to write the function you're talking about:
def spaced_word( s ):
return ' '.join( s )
print spaced_word( 'abcde' )
prints:
a b c d e
Edit: OK, now I looked at your code to see if I can answer your immediate question, and the answer is no, I can't. That code is just much too complicated. (Sorry!) It's the kind of code where the complexity should make you take a step back and ask, "Isn't there some easier way to do this?"
Am I unkind by saying this? If so, I sincerely apologize. But sometimes you just need to toss out your code and start over with a simpler approach. Yes, I know this from humbling personal experience, having thrown out a lot of my own code!
Some red flags:
There are two different places where a space is appended to the output string
There are three different places where a character gets appended to the output. The first character of the string gets special treatment, and so does the last.
A loop that runs to the string length minus two.
In Python, as in Ruby and JavaScript and probably quite a few other languages, when you need to insert something in between some other items, without an extra "something" at the end, the first place to look is the join() function or method. In each of those languages, that takes an array and inserts a string between each of the array elements. It's a great way to do this kind of operation.
Given that, then the only thing remaining is to turn the original string into an array (a list in Python). The way you do that varies. In Python, you could use list(string) - although you don't even need to do that, because Python strings are iterable and you can do a join on them directly just as you could do with a Python list. (Thanks to @Blckknght for pointing that out.) In Ruby or JavaScript, it's string.split('') - interestingly enough, the syntax is identical in those two languages.
Then you just take the result and do a join(' ') on it in the appropriate syntax for that language. The code is remarkably similar in all three languages.
Ruby:
def spaced_word( s )
s.split('').join(' ')
end
JavaScript:
function spacedWord( s ) {
return s.split('').join(' ');
}
Python:
def spaced_word( s ):
return ' '.join( s )
Or to make it a bit more similar to the other two languages:
def spaced_word( s ):
return ' '.join( list(s) )
This raises a (hopefully) interesting point. My first approach to the Python code was the second version with the explicit list(s). I've been coding in JavaScript, Python, and Ruby lately, but mostly the last week or so has been JavaScript. So when I wrote a bit of Python code, I naturally fell into a more JavaScript-style approach: first convert the string to an array (list) and then call join on it. In JavaScript, you can't call join directly on a string. But in Python you can!
Perhaps something similar happened in your code? The way you wrote it with a careful character-by-character loop is how you would do it in in C and some other languages. It's a natural tendency to carry over coding patterns from one language to another, just as I wrote my Python code from a JavaScript mindset.
For myself, I know I have to constantly fight that and think in terms of the language I'm currently coding in and what features it offers. Python, along with Ruby and JavaScript and other similar languages, offer a wealth of string and array/list manipulation functions, so that's the place to look before writing more detailed code of your own.