0

To be more specific, I'm trying to print several results of a function in the form of a sentence like so print str(function(first)) + str(function(second)) + str(function(third)) and so on, but I've come across the problem that there are no spaces between the individual strings.

So, what I want to know is how I add spaces between each of those strings.

3 Answers 3

5

use join()

print " ".join([str(function(first)), str(function(second)), str(function(third))])
Sign up to request clarification or add additional context in comments.

4 Comments

You can also pass join a generator expression rather than a list, if that's more convenient: " ".join(str(function(x)) for x in [first, second, third])
Alternatively, " ".join(map(lambda x: str(function(x)), (first, second, third)))
Since i'm new to join(), what advantages/disadvantages does it have when compared to just doing print function(first), function(second), etc.
It is faster. With '+' operator you create new string with each concatenation. str.join is optimized, so there is less memory allocation and deallocation. Also it is more 'pythonic'. You want do just that - join results of your functions in one sentence, right?
3
print function(first), function(second), function(third)

or use string formatting:

print '{0} {1} {2}'.format(function(first), function(second), function(third))

Comments

1
print str(function(first)) + ' ' + str(function(second)) + ' ' + str(function(third))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.