# Printing " surrounding a variable

**URL:** <https://discuss.python.org/t/printing-surrounding-a-variable/15431>\
**Category:** Python Help\
**Tags:** help\
**Created:** [May 2, 2022, 3:20pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431 "2022-05-02T15:20:43Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![pencilgraphite](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@pencilgraphite](https://discuss.python.org/u/pencilgraphite)\
**Post date:** [May 2, 2022, 3:20pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/1 "2022-05-02T15:20:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 2, 2022, 3:33pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/2 "2022-05-02T15:33:03Z")

</div>

What, like this…

```python
word = 'blue'
points = 10

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

```

---

<div class="post-metadata">

**Author:** ![ajoino](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ajoino/32/6907_2.png) [@ajoino](https://discuss.python.org/u/ajoino)\
**Post date:** [May 2, 2022, 3:43pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/3 "2022-05-02T15:43:43Z")

</div>

You can also use f-strings:

```python
print(f'"{word}" earned "{points}" points')

```

---

<div class="post-metadata">

**Author:** ![pencilgraphite](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@pencilgraphite](https://discuss.python.org/u/pencilgraphite)\
**Post date:** [May 2, 2022, 3:46pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/4 "2022-05-02T15:46:09Z")

</div>

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?

```auto
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()           

```

---

<div class="post-metadata">

**Author:** ![pencilgraphite](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@pencilgraphite](https://discuss.python.org/u/pencilgraphite)\
**Post date:** [May 2, 2022, 3:48pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/5 "2022-05-02T15:48:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 2, 2022, 3:50pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/6 "2022-05-02T15:50:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ajoino](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ajoino/32/6907_2.png) [@ajoino](https://discuss.python.org/u/ajoino)\
**Post date:** [May 2, 2022, 3:50pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/7 "2022-05-02T15:50:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [May 2, 2022, 4:01pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/8 "2022-05-02T16:01:15Z")

</div>

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):

```auto
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](https://docs.python.org/3/library/string.html#format-string-syntax)

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 2, 2022, 4:27pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/9 "2022-05-02T16:27:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pencilgraphite](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@pencilgraphite](https://discuss.python.org/u/pencilgraphite)\
**Post date:** [May 2, 2022, 4:30pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/10 "2022-05-02T16:30:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [May 2, 2022, 4:45pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/11 "2022-05-02T16:45:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pencilgraphite](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@pencilgraphite](https://discuss.python.org/u/pencilgraphite)\
**Post date:** [May 2, 2022, 4:53pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/12 "2022-05-02T16:53:39Z")

</div>

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.

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

```

Thank you all for the responses!!

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [May 2, 2022, 4:56pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/13 "2022-05-02T16:56:14Z")

</div>

```auto
print("Current hand:", end=" ")
display_hand(hand)

```

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 2, 2022, 6:14pm UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/14 "2022-05-02T18:14:34Z")

</div>

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.

```python
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.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [May 3, 2022, 1:06am UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/15 "2022-05-03T01:06:14Z")

</div>

All good 🙂

[Stop writing classes](https://www.youtube.com/watch?v=5ZKvwuZSiyc)

[Write moar classes](https://lucumr.pocoo.org/2013/2/13/moar-classes/)

[Execution in the Kingdom of Nouns](http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html)

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [June 22, 2024, 3:05am UTC](https://discuss.python.org/t/printing-surrounding-a-variable/15431/18 "2024-06-22T03:05:39Z")

</div>


