1

I'm trying to print two output statements on the same line in Python 2.7 (or 3.4). The print statements don't come one after the other Like print a/ print b. My program takes 329 (say) and returns that value in words - three hundred twenty nine. So it will determine the 300 part and print it then the 29 part and print that.

if (main == hundreds[index]):
    print hundreds_words[index]
for location in range (0, 10):
    if (difference == twenties[location]):
        print twenties_words[location]

I want to print the twenty nine on the same line as the three hundred. I suppose I could try and rig up a solution but I would like to know if Python has a procedure to do that.

4 Answers 4

2

Yes, it does. You just need to tell print not to add a new-line after the first one. In python2, you do this by adding a trailing comma: print mystring,. In python3, print is a function that has the end keyword argument: print(mystring, end="")

Sign up to request clarification or add additional context in comments.

Comments

1

The easy way is to rewrite your number-to-words function and have it return a string instead of printing it.

The more involved way is to redirect stdout to capture the print output as a string.

Edit: looks like I made it more complicated than necessary; you could instead try

output_words = []
if (main == hundreds[index]):
    output_words.append(hundreds_words[index])
for location in range (0, 10):
    if (difference == twenties[location]):
        output_words.append(twenties_words[location])
return " ".join(output_words)

Comments

0

In python 2 you can end a print statement with a , to indicate not to terminate in a newline, e.g.:

print hundreds_words[index], 

In python 3 (or py2 with from __future__ import print_function) you explicitly need to define the end, e.g.:

print(hundreds_words[index], end=' ')

But ideally you would just collect all the result up in a list and join() them at the end...

result = []
if (main == hundreds[index]):
    result.append(hundreds_words[index])
for location in range (0, 10):
    if (difference == twenties[location]):
        result.append(twenties_words[location])

print(' '.join(result))

Comments

0

Always design functions to return values, and not to print them (it's much more Pythonic!). Therefore, you should modify your code this way:

# FOR PYTHON 2.x

if (main == hundreds[index]):
    print hundreds_words[index], # Note the comma
for location in range (0, 10):
    if (difference == twenties[location]):
        print twenties_words[location]

# FOR PYTHON 3.x

if (main == hundreds[index]):
    print(hundreds_words[index], end=" ") # end=" " will override the default '\n' and leave a space after the string
for location in range (0, 10):
    if (difference == twenties[location]):
        print(twenties_words[location])

There's also a third option, more scalable: to wrap all datas in a list, and then print everything.

printable = []

if (main == hundreds[index]):
    printable += hundreds_words[index]
for location in range (0, 10):
    if (difference == twenties[location]):
        printable += twenties_words[location]

print(" ".join(printable))

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.