Having dictionary keys that are strings print without quotation marks

How do I get the keys in a dictionary to print without a quotation mark? I am making a poker game where everyone’s hand is a dictionary. The key is a letter to reference when selecting a card, which is the corresponding value. This is the deal function in my game.

    def deal(self):
        alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']
        for i in range(len(new_deck.cards)):
            if i % 4 == 4 or i % 4 == 0:
                player1.hand[alphabet[len(player1.hand)]] = new_deck.cards[i]
            elif i % 4 == 3:
                player2.hand[alphabet[len(player2.hand)]] = new_deck.cards[i]
            elif i % 4 == 2:
                player3.hand[alphabet[len(player3.hand)]] = new_deck.cards[i]
            elif i % 4 == 1:
                player4.hand[alphabet[len(player4.hand)]] = new_deck.cards[i]

When I print the resulting hand for player 1 I get,
{‘A’: Ace-of-Spades, ‘B’: Queen-of-Diamonds, ‘C’: 3-of-Diamonds, ‘D’: 10-of-Spades, ‘E’: 4-of-Diamonds, ‘F’: 8-of-Hearts, ‘G’: 4-of-Clubs, ‘H’: 5-of-Hearts, ‘I’: 7-of-Hearts, ‘J’: 3-of-Spades, ‘K’: 4-of-Spades, ‘L’: 6-of-Spades, ‘M’: 9-of-Hearts}

I think that the quotation around the letter looks clunky and I am trying to remove them but nothing I do seems to work.

The print function uses an object’s __str__ method if possible and __repr__ if not. You could override __str__ to give you whatever kind of output you like (or even consider a new method for an output).

2 Likes

I’m still not quite clear on this.

When I run

print(alphabet[0])

I get
A
how come there are no quotes around this output?

That’d be the __str__ representation of a string object. The __repr__ of the same would be 'a' instead of a. I’d assume the default for most containers like dicts is to use the .__repr__ representation of the objects within that container.

print('a') print(str('a')) print(repr('a'))
1 Like

So I modified it to this…

    def deal(self):
        alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']
        for i in range(len(new_deck.cards)):
            if i % 4 == 4 or i % 4 == 0:
                player1.hand[str(alphabet[len(player1.hand)])] = new_deck.cards[i]
            elif i % 4 == 3:
                player2.hand[str(alphabet[len(player2.hand)])] = new_deck.cards[i]
            elif i % 4 == 2:
                player3.hand[str(alphabet[len(player3.hand)])] = new_deck.cards[i]
            elif i % 4 == 1:
                player4.hand[str(alphabet[len(player4.hand)])] = new_deck.cards[i]

How come this does not work?

Ah, for some reason I thought you were dealing with a class in the first place but you’re not which makes this trickier, sorry. It’s not the element stored in the dictionary that’s the issue it’s how the dictionary object represents those elements when you print it (it uses their .__repr__ method rather than .__str__) which is unlike how print works for a single object.

It’s the same as something like the following-

print('a') print(['a']) # now that string is inside a container # the representation is that of repr(obj)

It’s the container object that’s responsible for the final representation of the container itself so it’s not an easy one to get around (changing the inputs like in your example is not going to get you what you want).

If you’re familiar with classes then you could consider sub-classing the dictionary and overwriting the __str__ method.

If you’re not then consider something like a loop which accesses each key/item pair in turn and trying to print with those (multiple prints might be easier).

Perhaps the looping and printing could be a new function to make it more convenient to use but I can’t see any way around changing the representation without modifying either the inputs or the dictionary using classes.