Printing " surrounding a variable

If I were to want my output to look like this. “blue” earned 10 points:

blue is a variable not containing the quotation marks.

How would I write this in a print statement?

Where using a line like this results in extra spacing between the quotes and word.

print(’"’,word,’"’( <----- 2 single quotes surrounding a double quote using commas), “Earned”, get_word_score(word,n), “points,”, “Total Score:”, total)

But not using commas python tries to recognize word as a non-variable?

What, like this…

word = 'blue'
points = 10

print('"'+word+'" earned',points,'points')

You can also use f-strings:

print(f'"{word}" earned "{points}" points')
1 Like

Yes, concatenate worked for me. I know it’s fairly simple, but I guess I’ve never used concatenate in a print statement, so it threw me off lol. Was drawing a blank.

Also, if I wanted to print these on the same line how would I do that?

print(“Current hand:”), display_hand(hand) ------- These print on separate lines “Current hand” prints before the function.

print(“Current hand:”, display_hand(hand) ) ----- and this prints the return of the function which is None

Where display_hand(hand) function returns None but prints from the function itself?

def display_hand(hand):
  
    for letter in hand.keys():
        for j in range(hand[letter]):
             print(letter, end=' ')      # print all on the same line
    print()           

I’ve never seen this syntax before, but I found some documentation on f-strings that I will read and I appreciate you letting me know of it.

1 Like

When I want any output to have a specific format, I tend to build a ‘output’ string, then print(output) rather than try to do it on-the-fly within the print() function.

Glad I could help, I think f-strings are the best thing since sliced bread

What you seem to be looking for is called “string formatting,” a way
of templating a string with placeholders for the things you want to
substitute. There are several kinds of string formatting supported
in modern versions of Python, but the current recommendations in
Python’s official documentation are for {}-style formatting
(additional indentation provided for clarity):

print(
    '"{}" Earned {} points, Total Score: {}'.format(
        word,
        get_word_score(word, n),
        total,
        )
    )

You can find a detailed specification as well as many varied
examples here:
https://docs.python.org/3/library/string.html#format-string-syntax

1 Like

Building on this, one could define a class to format the output, then pass the variables to said class, right?

Thank you, I have pinned these docs as well. Simply using concatenate worked for the first issue, although I know going forward, I may need to learn these different string formatting methods mentioned here. I still haven’t found the right syntax for calling this display_hand(hand) function and printing “Current hand:” on the same line. I know I could do this from within the function or I could return something from the function and print it that way but I’d rather not for now if I can find a different way.

The real issue seems to be trying to do this while returning None on my function though.

Why use a class?

Python already has five ways of formatting strings:

  • concatenating substrings with +
  • C-style string interpolation with %
  • the format() method on strings
  • the Template class from the string module
  • f-strings

I guess if your formatting is really complex, you might need a class to handle it. E.g. laying text out in tables.

I guess you can just add the (end = ’ ') argument to a print statement followed by a function call like what was already being used in the function itself. Doesn’t have to be two print statements.

print("Current hand:", end =  '  ')
display_hand(hand)

Thank you all for the responses!!

print("Current hand:", end=" ")
display_hand(hand)
1 Like

I’m simply exploring the possibilities really. Maybe using a class is not needed as the method is already covered by one (or more) of the five ways your PB list, three of which I’ve come across before, but I’ve only ever used two of them in any of my own code.

I’m trying to get out of the habit of relying on my ‘old school’ ways and learn how to use Python in a way that it’s designed to be used.

Anyway, I’ve cobbled this together as a working example of my thoughts and based on this thread.

class Score:
  def __init__(self, word, points):
    self.word = word
    self.points = points
  
  def output(self):
    print(f'"{self.word}" earned {self.points} points')

player1 = Score("blue", 10)
player1.output()

RFC.
Thanks.

1 Like

All good :slight_smile:

Stop writing classes

Write moar classes

Execution in the Kingdom of Nouns

1 Like