Skip to content

cvzi/flag

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

flag

foodemoji on PyPI Python Versions Coverage Status Codacy Badge Maintainability Quality Gate Status

Flag emoji for Python.
Converts flag emoji to ASCII and other way round.

This is based on http://schinckel.net/2015/10/29/unicode-flags-in-python/ by schinckel

How it works

All the flag emoji are actually composed of two unicode letters. These are the 26 regional indicator symbols.

Alone they look like this:
๐Ÿ‡ฆ ๐Ÿ‡ง ๐Ÿ‡จ ๐Ÿ‡ฉ ๐Ÿ‡ช ๐Ÿ‡ซ ๐Ÿ‡ฌ ๐Ÿ‡ญ ๐Ÿ‡ฎ ๐Ÿ‡ฏ ๐Ÿ‡ฐ ๐Ÿ‡ฑ ๐Ÿ‡ฒ ๐Ÿ‡ณ ๐Ÿ‡ด ๐Ÿ‡ต ๐Ÿ‡ถ ๐Ÿ‡ท ๐Ÿ‡ธ ๐Ÿ‡น ๐Ÿ‡บ ๐Ÿ‡ป ๐Ÿ‡ผ ๐Ÿ‡ฝ ๐Ÿ‡พ ๐Ÿ‡ฟ

If you pair them up according to ISO 3166 some browsers and phones will display a flag.
For example TW is Taiwan: ๐Ÿ‡น + ๐Ÿ‡ผ = ๐Ÿ‡น๐Ÿ‡ผ

So, to encode an ASCII code like :TW: to ๐Ÿ‡น๐Ÿ‡ผ, we just need to convert the ASCII T and W to the corresponding regional indicator symbols ๐Ÿ‡น and ๐Ÿ‡ผ.
To reverse it, we translate the regional indicator symbols back to ASCII letters.

Example

    >>> import flag
    
    >>> flag.flag("IL")
    '๐Ÿ‡ฎ๐Ÿ‡ฑ'
    
    >>> flag.flag("GBENG")
    '๐Ÿด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟ'
    
    >>> flag.flagize("Flag of Israel :IL:")
    'Flag of Israel ๐Ÿ‡ฎ๐Ÿ‡ฑ'
    
    >>> flag.dflagize("Flag of Israel ๐Ÿ‡ฎ๐Ÿ‡ฑ")
    'Flag of Israel :IL:'
    
    >>> flag.flagize("England :gb-eng: is part of the UK :GB:", subregions=True)
    'England ๐Ÿด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟ is part of the UK ๐Ÿ‡ฌ๐Ÿ‡ง'
    
    >>> flag.dflagize("England ๐Ÿด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟ is part of the UK ๐Ÿ‡ฌ๐Ÿ‡ง", subregions=True)
    'England :gb-eng: is part of the UK :GB:'

    >>> my_flags = flag.Flag(only_supported=True, allow_subregions=True)
    >>> my_flags.flagize("Convert actual flags like :US: but not unsupported ones like :XX:")
    'Convert actual flags like ๐Ÿ‡บ๐Ÿ‡ธ but not unsupported ones like :XX:'

Install

pip install emoji-country-flag

See: https://pypi.org/project/emoji-country-flag/

Python 3.10 or higher is required for the latest release. The last release for Python 3.7 to 3.9 was v1.3.2. The last release for Python 2.7, 3.4 and 3.5 was v1.2.4.

Documentation

Documentation Status

Documentation at https://flag.readthedocs.io