-1

Sorry if this has been asked before, I have looked but I can't find the answer I want. I'm creating a card class for a game and I have this for the str definition:

def __str__(self):
      return self.rank + " of " + self.suit

Which works, but is printing the cards like this,

Two of Hearts 

Three of Hearts

Four of Hearts

Five of Hearts

I have seen a method for printing extra space into a string and I want it to print it with a couple of extra spaces so that the "of rank" parts of the string all line up. Unfortunately I can't remember what the method is, nor what it's called, so I'm having difficulty finding it again.

Also bonus question, I'm having difficulty demonstrating what I mean on Stack Overflow because it doesn't seem to want to let me place lines of text directly underneath other lines of text without making it a whole new paragraph, and additionally doesn't let me add extra spaces between words. I get that this is probably to help with question formatting and making sure they don't take up too much space with junk, but it makes demonstrating what I mean difficult, is there any way to get around that?

Anass ABEA
  • 479
  • 5
  • 14

3 Answers3

2

in case the first words aren't much more than x (x depends on the IDE you are using it's normally between 2 and 4 in my case it's 4) char appart use indentation "\t" like so:

Simple function with example:

def printer (rank,suit):
    print(rank + "\tof\t" + suit)
    
printer("Two","Hearts")
printer("Three","Hearts")
printer("Four","Hearts")
printer("Five","Hearts")

Result :

Two     of      Hearts                                                                 
Three   of      Hearts                                                                 
Four    of      Hearts                                                                 
Five    of      Hearts

EDIT : if it didn't work for you feel free to set a max number of char like for example 10 and based on the length of the string adds the rest in spaces Example:

def printer (rank,suit):
    spaces = 10
    print(rank +" "*(10-len(rank))+ " of " + suit)
Anass ABEA
  • 479
  • 5
  • 14
1

Previous solution probably works in your case. However, as a note, simply using a tab would work best for cases where the earlier "columns" are all about the same length.

  • There's a built-in method str.rjust
  • You can automatically infer the amount of space to add, if you have a list of values. This is a good use case for Enum.
  • As mentioned, the simpler tab or hard-coded number-of-spaces solutions might work for your use case already, but this is more general and works even when possible values are of very different lengths.

Here's an example with a fictitious value added for demonstration of the point (some code was used from here).

from enum import Enum
class CardValue(Enum):
    Ace = 1
    Two = 2
    Three = 3
    Four = 4
    Five = 5
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = 11
    Queen = 12
    King = 13
    SomeOtherStrangeValue = 14


class Card:
    value_max_len = max(len(v) for v in CardValue._member_map_.keys()) # will be 21
    def __init__(self, value, suit):
        # can also make suit an enum and do the same thing
        assert isinstance(value, CardValue)
        self.value = value
        self.suit = suit

    def __str__(self):
        return "%s of %s" % (str.ljust(self.value.name, Card.value_max_len), self.suit)

print(str(Card(CardValue.Ace, "Heart")) + "\n" + str(Card(CardValue.SomeOtherStrangeValue, "Heart")))

Output:

Ace                   of Heart
SomeOtherStrangeValue of Heart
ELinda
  • 2,658
  • 1
  • 10
  • 9
0

The answers above both worked fine, although they weren't the specific function I was looking for.

The function I was looking for prints a specific number of characters and fills in any additional characters with spaces using an f' formatted string. You place the first word of your sentence where I placed the word "here" in curly braces {here:>7}. You follow this by a colon, and either the symbol > or < depending on which direction you want the white space to extend. if you wanted the white space to extend after your sentence you would use the last word of your sentence for "here" instead of the first. Then you add the total number of characters you want to be printed and finally close the curly brace and continue with whatever else you want to print. Very simple.

My code now looks like this:

def __str__(self):
        return f'{self.rank:>7} of {self.suit}'

and prints like this:

  Seven of Hearts
    Six of Diamonds
   Nine of Clubs
   Nine of Diamonds
  Eight of Spades
    Ace of Diamonds
   Five of Spades
    Ace of Spades
   Jack of Clubs

I found the answer in this stackoverflow thread How do I print an integer with a set number of spaces before it?