Python printing two things on one line

587
February 22, 2017, at 11:03 PM

I'm working through the edx python course. I've done everything correctly in the last assignment. However, I'm struggling with this print issue.

I have a function displayHand(hand) which essentially takes a dictionary with keys being letters and values be the number of occurrences of the letters. The function then prints a string with each key appearing the same number of times as its value. So for example if

hand={a:3, b:4, c:1}
displayHand(hand)=a a a b b b b c

Now in the program for any given hand it wants me to display it using the displayHand function with the text "current hand: "

Again, if hand={a:3, b:4, c:1} the program should display

current hand: a a a b b b b c

How do I do this with a print statement? I've tried print("current hand"+str(displayHand(hand)), but this first evaluates the function and then just prints None. I can't put print("Current Hand: ") and displayHand(hand) directly underneath, because then they print on different lines. I need to get them to print on the same line.

Answer 1

Python 3:

def displayHand(dct):
    for key, val in sorted(dct.items()):  # sorted if key order should be abc
        print((key + " ") * val, end="")

hand = {"a": 3, "b": 4, "c": 1}
print("current hand: ", end="")
displayHand(hand)

Notice the end="" which appends whatever is inside "". The default setting is end="\n". In this case, we will just use an empty string. See here and here.

Output:

current hand: a a a b b b b c 
Answer 2

That should work for you :

print("current hand: ", end="")
displayHand(hand)

In print() function default end is new line. In the code above you just change it to whatever you want.

Answer 3

You could do the following:

def displayHand(hand):
    print('{} {}'.format('current hand:', ' '.join([' '.join([li] * ni) for li, ni in sorted(hand.items())])))

That creates a string first which is then printed.

Then

displayHand(hand)

prints the desired output:

current hand: a a a b b b b c
Answer 4

You can use print twice, you just have to add a comma after the first print ex: print("a"), print("b").

Answer 5

Check out this thread, it shows some examples of using commas at the end of the line and other sys tools

READ ALSO
Restrict Disqus API query by language

Restrict Disqus API query by language

So I've been testing out the various methods through the python bindings and furiously reading the documentationHope to soon put the components and functions into a larger script to crawl through a series of queries, but am starting to wonder about the limitations...

424
How do i let my code add strings in Python

How do i let my code add strings in Python

So for a project of mine I need to have my code generate a number of strings

437
Require POST parameters in Flask-RESTful [on hold]

Require POST parameters in Flask-RESTful [on hold]

What is the correct way to require POST parameters in Flask-RESTful? For now I'm using parseradd_argument but I'm not sure this is the right way of doing it

486