Open In App

How to Print a Deck of Cards in Python

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This article teaches how to Print a Deck of Cards using Python. The most popular deck of cards used today is a normal 52-card deck of French-suited playing cards. It is the only traditional pack[b] used for playing cards in English-speaking nations, but in many other nations throughout the world, it is used alongside other traditional, frequently older, standard packs with various suit systems, as those with German-, Italian-, Spanish-, or Swiss suits. The English pattern pack is the only one regularly accessible in Britain and the US and the most popular pattern of French-suited cards globally. 

In a deck, there are 52 cards comprising 4 Suits and 13 ranks. Because there are 4 Suits and 13 ranks, the total number of cards is 13*4 i.e. 52.

The four suits are: Club, Diamond, Heart, Spade 

These are represented by the following symbols respectively:  â™£ , ♦ , ♥ , â™ 

And the ranks of the cards are: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K

Print a Deck of Cards using Python

Create a list of 4 different Suits. Create a list of 13 different ranks. Iterate over the list of ranks, For each iteration, we loop over all the suits. Finally, we print each pair of suits and ranks. This will print all 52 cards. Following is an implementation of the above approach:

Python3




# a list of all the suits
Suits = ['CLUB', 'DIAMOND',
         'Heart', 'SPADE']
# a list of all the ranks
Ranks = ['A', '2', '3', '4',
         '5', '6', '7', '8',
         '9', '10', 'J', 'Q', 'K']
  
# Matching all the suits with all the ranks
for rank in Ranks:
  
    for suit in Suits:
        print(f'{rank} of {suit}'.ljust(16), end='')
  
    # Spacer
    print()


Output:

A of CLUB       A of DIAMOND    A of Heart      A of SPADE      
2 of CLUB       2 of DIAMOND    2 of Heart      2 of SPADE      
3 of CLUB       3 of DIAMOND    3 of Heart      3 of SPADE      
4 of CLUB       4 of DIAMOND    4 of Heart      4 of SPADE      
5 of CLUB       5 of DIAMOND    5 of Heart      5 of SPADE      
6 of CLUB       6 of DIAMOND    6 of Heart      6 of SPADE      
7 of CLUB       7 of DIAMOND    7 of Heart      7 of SPADE      
8 of CLUB       8 of DIAMOND    8 of Heart      8 of SPADE      
9 of CLUB       9 of DIAMOND    9 of Heart      9 of SPADE      
10 of CLUB      10 of DIAMOND   10 of Heart     10 of SPADE     
J of CLUB       J of DIAMOND    J of Heart      J of SPADE      
Q of CLUB       Q of DIAMOND    Q of Heart      Q of SPADE      
K of CLUB       K of DIAMOND    K of Heart      K of SPADE 

Print a Deck of Cards along with their Unicode symbols in Python

Create a list of 4 different Suits containing Unicode characters of the suits’ symbols. Now the approach should be similar to the one above. Create a list of 13 different ranks. Iterate over the list of Suits. For each iteration, we loop over all the ranks. Finally, we print each pair of suits and ranks. This will print all 52 cards.

Python3




# a list of all the suits
Suits = ["\u2663", "\u2665",
         "\u2666", "\u2660"]
# a list of all the ranks
Ranks = ['A', '2', '3', '4', '5',
         '6', '7', '8', '9', '10',
         'J', 'Q', 'K']
  
# Matching all the suits with all the ranks
for rank in Ranks:
  
    for suit in Suits:
        print(f'{rank} of {suit}'.ljust(10), end='')
  
    # Spacer
    print()


Output:

A of ♣    A of ♥    A of ♦    A of ♠    
2 of ♣    2 of ♥    2 of ♦    2 of ♠    
3 of ♣    3 of ♥    3 of ♦    3 of ♠    
4 of ♣    4 of ♥    4 of ♦    4 of ♠    
5 of ♣    5 of ♥    5 of ♦    5 of ♠    
6 of ♣    6 of ♥    6 of ♦    6 of ♠    
7 of ♣    7 of ♥    7 of ♦    7 of ♠    
8 of ♣    8 of ♥    8 of ♦    8 of ♠    
9 of ♣    9 of ♥    9 of ♦    9 of ♠    
10 of ♣   10 of ♥   10 of ♦   10 of ♠   
J of ♣    J of ♥    J of ♦    J of ♠    
Q of ♣    Q of ♥    Q of ♦    Q of ♠    
K of ♣    K of ♥    K of ♦    K of ♠ 

Performing a shuffle in the deck of cards using Python

First, we will create all the cards using the multiplication method from the itertools module and store them in a List. Then, we will use the shuffle method of the random module to shuffle the list randomly. Finally, we will print out the resulting deck.

Python3




from itertools import product
from random import shuffle
  
# a list of all the suits
Suits = ["\u2663", "\u2665",
         "\u2666", "\u2660"]
# a list of all the ranks
Ranks = ['A', '2', '3', '4', '5',
         '6', '7', '8', '9', '10',
         'J', 'Q', 'K']
  
deck = list(product(Ranks, Suits))
shuffle(deck)
  
for i in range(0, len(deck), 4):
    print("{} {} {} {}".format(
        deck[i][0] + deck[i][1],
        deck[i+1][0] + deck[i+1][1],
        deck[i+2][0] + deck[i+2][1],
        deck[i+3][0] + deck[i+3][1]
    ))


Output:

2♠ 5♥ 2♣ 10♠
K♥ 7♣ Q♦ 8♠
K♦ 7♥ 6♠ 6♣
J♣ 10♥ 9♦ 5♦
10♣ 8♥ 6♥ J♦
Aâ™  Kâ™  Jâ™  7â™ 
K♣ Q♥ Q♣ J♥
A♥ 4♣ 3♥ 7♦
8♦ 8♣ 4♠ 3♦
6♦ 2♦ A♣ 10♦
9♣ 2♥ 5♣ Q♠
A♦ 9♥ 3♣ 5♠
3♠ 4♥ 9♠ 4♦


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads