Flags

Get the flag emoji of a country using its 2 (or 3)-letter ISO country code

source

flag

 flag (cc)

Get the flag emoji of a country using its 2 (or three)-letter code.

Type Details
cc str Country code, a string of two letters
Returns str The Unicode emoji flag of the respective country

Get some country flags

flag('pt'), flag('prt')
('πŸ‡΅πŸ‡Ή', 'πŸ‡΅πŸ‡Ή')
flag('jp'), flag('jpn'), flag('JPN'), flag("JAPAN"), flag('japan')
('πŸ‡―πŸ‡΅', 'πŸ‡―πŸ‡΅', 'πŸ‡―πŸ‡΅', 'πŸ‡―πŸ‡΅', 'πŸ‡―πŸ‡΅')
flag('za')
'πŸ‡ΏπŸ‡¦'

Use in a Jupyter notebook

The following html and md functions enable HTML and markdown output to contain dynamic values.

from IPython.display import display_html as html, display_markdown as md
from functools import partial

html = partial(html, raw=True)
md = partial(md, raw=True)

Use in a markdown string

md(f'# {flag("pr")}')

πŸ‡΅πŸ‡·

Use in an HTML string for more granular control

html(f'<h1 style="font-size: 40pt; color: steelblue;">This is the flag of Sweden: <span style="font-size: 60pt;">{flag("se")}</span></h1>')

This is the flag of Sweden: πŸ‡ΈπŸ‡ͺ

Using flags in Plotly charts

population = pd.DataFrame({
    'country': ['China', 'India', 'United States', 'Indonesia', 'Pakistan'],
    'population': [1439323776, 1380004385, 331002651, 273523615, 220892340],
    'code': ['cn', 'in', 'us', 'id', 'pk']
})
population
country population code
0 China 1439323776 cn
1 India 1380004385 in
2 United States 331002651 us
3 Indonesia 273523615 id
4 Pakistan 220892340 pk
fig = px.bar(
    population,
    x='country',
    y='population',
    height=500, 
    text=[flag(cc) for cc in population['code']],
    template='ggplot2'
)
fig.data[0].textfont.size = 35
fig.data[0].marker.color = 'steelblue'
fig
fig = px.bar(
    population.sort_values('population'),
    y='country',
    x='population',
    orientation='h',
    height=500, 
    opacity=0.8,
    text=[flag(cc) for cc in population.sort_values('population')['code']],
    template='ggplot2'
)
fig.data[0].textfont.size = 35
fig.data[0].marker.color = 'teal'
fig
europe = gapminder().query('year==2007').query('continent=="Europe"')
europe['flag'] = [flag(cc) for cc in europe['iso_alpha']]
europe.head()
country continent year lifeExp pop gdpPercap iso_alpha iso_num flag
23 Albania Europe 2007 76.423 3600523 5937.029526 ALB 8 πŸ‡¦πŸ‡±
83 Austria Europe 2007 79.829 8199783 36126.492700 AUT 40 πŸ‡¦πŸ‡Ή
119 Belgium Europe 2007 79.441 10392226 33692.605080 BEL 56 πŸ‡§πŸ‡ͺ
155 Bosnia and Herzegovina Europe 2007 74.852 4552198 7446.298803 BIH 70 πŸ‡§πŸ‡¦
191 Bulgaria Europe 2007 73.005 7322858 10680.792820 BGR 100 πŸ‡§πŸ‡¬
fig = px.scatter(
    europe,
    x='gdpPercap',
    y='lifeExp',
    height=600,
    text='flag',
    hover_data=['lifeExp',  'pop','gdpPercap'],
    hover_name=europe['flag'].add(' ').add(europe['country']),
    title=f'Life expectancy ~ GDP per capita<br><b>Europe 2007</b>',
    template='plotly_white',
    log_y=False)
fig.data[0].textfont.size = 35
fig.data[0].hoverlabel.bgcolor = 'white'
fig.data[0].hoverlabel.font.size = 20
fig