flag๏
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
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:'
Install๏
pip install emoji-country-flag
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 R to the corresponding regional indicator symbols ๐น and ๐ผ.
To reverse it, we translate the regional indicator symbols back to ASCII letters.
Functions๏
- flag.flag(countrycode: str) str [source]๏
Encodes a single flag to unicode. Two letters are converted to regional indicator symbols Three or more letters/digits are converted to tag sequences. Dashes, colons and other symbols are removed from input, only a-z, A-Z and 0-9 are processed.
In general a valid flag is either a two letter code from ISO 3166 (e.g.
GB
), a code from ISO 3166-2 (e.g.GBENG
) or a numeric code from ISO 3166-1. However, not all codes produce valid unicode, see http://unicode.org/reports/tr51/#flag-emoji-tag-sequences for more information. From ISO 3166-2 only Englandgbeng
, Scotlandgbsct
and Walesgbwls
are considered RGI (recommended for general interchange) by the Unicode Consortium, see http://www.unicode.org/Public/emoji/latest/emoji-test.txt- Parameters:
countrycode (str) โ Two letter ISO 3166 code or a regional code from ISO 3166-2.
- Returns:
The unicode representation of the flag
- Return type:
str
- flag.flag_safe(countrycode: str, unsupported: Callable[[MutableMapping[str, str | bool | None]], Any] | Literal['error', 'allow'] = 'error', invalid: Callable[[MutableMapping[str, str | bool | None]], Any] | Literal['error', 'allow'] = 'error', custom_data: MutableMapping[str, MutableMapping[str, str | bool | None]] | None = None) Any [source]๏
Encodes a single flag to unicode. Two letters are converted to regional indicator symbols Three or more letters/digits are converted to tag sequences. Dashes, colons and other symbols are removed from input, only a-z, A-Z and 0-9 are processed.
The
unsupported
andinvalid
parameter control which flags are converted. If a flag is not supported or invalid, the function will return the result of theon_illegal
function or raise an error if either parameter is set to โerrorโ.- Parameters:
countrycode (str) โ Two letter ISO 3166 code or a regional code from ISO 3166-2.
unsupported (str|callable) โ
โerrorโ | โallowโ | Callable(dict)
If โallowโ return the flag. If โerrorโ raise an error if flag is unsupported. If callable return the result of the callable if flag is unsupported. The callable receives the result of the
flag.info()
function e.g.{'id_status': 'regular', 'supported': False, 'valid': True}
invalid (str|callable) โ Same as
unsupported
but for invalid flagscustom_data (dict) โ Custom maps dict with custom valid/supported values to overwrite the standard values. For example with the WhatsApp Texas flag:
{'XT': {'id_status': 'regular', 'supported': False, 'valid': True}}
- Returns:
The unicode representation of the flag
- Return type:
str
- flag.flagize(text: str, subregions: bool = False) str [source]๏
Encode flags. Replace all two letter codes
:XX:
with unicode flags (emoji flag sequences)- Parameters:
text (str) โ The text
subregions (bool) โ Also replace subregional/subdivision codes
:xx-xxx:
with unicode flags (flag emoji tag sequences).
- Returns:
The text with all occurrences of
:XX:
replaced by unicode flags- Return type:
str
- flag.dflagize(text: str, subregions: bool = False) str [source]๏
Decode flags. Replace all unicode country flags (emoji flag sequences) in text with ascii two letter code
:XX:
- Parameters:
text (str) โ The text
subregions (bool) โ Also replace subregional/subdivision flags (flag emoji tag sequences) with
:xx-xxx:
- Returns:
The text with all unicode flags replaced by ascii sequence
:XX:
- Return type:
str
- flag.flagize_subregional(text: str) str [source]๏
Encode subregional/subdivision flags. Replace all regional codes
:xx-xxx:
with unicode flags (flag emoji tag sequences)- Parameters:
text (str) โ The text
- Returns:
The text with all occurrences of
:xx-xxx:
replaced by unicode flags- Return type:
str
- flag.dflagize_subregional(text: str) str [source]๏
Decode subregional/subdivision flags. Replace all unicode regional flags (flag emoji tag sequences) in text with their ascii code
:xx-xxx:
- Parameters:
text (str) โ The text
- Returns:
The text with all regional flags replaced by ascii sequence
:xx-xxx:
- Return type:
str
- flag.info(countrycode: str, extended: bool = False) MutableMapping[str, str | bool | None] [source]๏
Retrieve information about a country code. E.g.
{'id_status': 'regular', 'supported': False, 'valid': True}
- flag.infos(extended: bool = False) MutableMapping[str, MutableMapping[str, str | bool | None]] [source]๏
Dict containing all supported and valid country codes. If extended is True, the dict will contain all country and subregional codes.
- flag.version() Mapping[Literal['module', 'cldr', 'emoji'], str] [source]๏
Return the version of this module, the Unicode CLDR version of the data and the Unicode Emoji version of the validity data.
- class flag.Flag(prefix_str: str = ':', suffix_str: str = ':', only_supported: bool = True, only_valid: bool = True, allow_subregions: bool = True, warn: bool = True)[source]๏
This class offers different prefix and suffix instead of colons and allows to only convert widely supported or valid flags.
- __init__(prefix_str: str = ':', suffix_str: str = ':', only_supported: bool = True, only_valid: bool = True, allow_subregions: bool = True, warn: bool = True) None [source]๏
Set a custom prefix and suffix. Instead of
:XY:
it will use{prefix}XY{suffix}
.To encode subregional flags, use a suffix that is either longer than 4 characters or that does not contain A-Z, a-z, 0-9 and does not start with a - (minus).
- Parameters:
prefix_str (str) โ The leading symbols
suffix_str (str) โ The trailing symbols
only_supported (bool) โ Only convert country codes that are widely supported by browsers and devices
only_valid (bool) โ Only convert country codes that are considered โvalidโ by Unicode
allow_subregions (bool) โ Also replace subregional/subdivision codes e.g. Scottland as :gb-sct:
warn (bool) โ Show a warning if an unsafe prefix or suffix is used
- add_flag(countrycode: str, supported: bool = True, valid: bool = True)[source]๏
Add a custom flag. This can be used to overwrite the standard values and allow custom flags to be considered supported and/or valid or remove supported flags. The current use-case for this function is the flag of Texas, which is only supported by WhatsApp. It is also possible to censor a flag. Example to add Texas flag:
flag.add_flag("XT", supported=True, valid=True)
Example to censor Italy flag:flag.add_flag("IT", supported=False, valid=False)
- Parameters:
text (str) โ The text
handle_illegal (callable) โ See flag.flagize()
- Returns:
The text with all regional flags replaced by ascii sequence
{prefix}xx-xxx{suffix}
- Return type:
str
- dflagize(text: str, handle_illegal: Callable[[str, str], str] | None = None) str [source]๏
Decode flags. Replace all unicode country flags (emoji flag sequences) in text with ascii two letter code
{prefix}XX{suffix}
Depending on the settings, the function may only convert supported or valid flags. If an unsupported or invalid flag is found, the flag is either ignored or
handle_illegal
is called.- Parameters:
text (str) โ The text
handle_illegal (callable) โ A function that is called when an illegal flag is found. The function should take two arguments, the unicode flag and the code i.e. XY and return a string to replace the illegal flag.
- Returns:
The text with all unicode flags replaced by ascii sequence
{prefix}XX{suffix}
- Return type:
str
- dflagize_subregional(text: str, handle_illegal: Callable[[str, str], str] | None = None) str [source]๏
Decode subregional/subdivision flags. Replace all unicode regional flags (flag emoji tag sequences) in text with their ascii code
{prefix}xx-xxx{suffix}
- Parameters:
text (str) โ The text
handle_illegal (callable) โ See flag.flagize()
- Returns:
The text with all regional flags replaced by ascii sequence
{prefix}xx-xxx{suffix}
- Return type:
str
- flagize(text: str, handle_illegal: Callable[[str, str], str] | None = None) str [source]๏
Encode flags. Replace all two letter codes
{prefix}XX{suffix}
with unicode flags (emoji flag sequences)For this method the suffix should not contain A-Z, a-z or 0-9 and not start with a - (minus).
Depending on the settings, the function may only convert supported or valid flags. If an unsupported or invalid flag is found, the flag is either ignored or
handle_illegal
is called.- Parameters:
text (str) โ The text
handle_illegal (callable) โ A function that is called when an illegal flag is found. The function should take two arguments, the match i.e. :XY: and the code i.e. XY and return a string to replace the illegal flag.
- Returns:
The text with all occurrences of
{prefix}XX{suffix}
replaced by unicode flags- Return type:
str
- flagize_subregional(text: str, handle_illegal: Callable[[str, str], str] | None = None) str [source]๏
Encode subregional/subdivision flags. Replace all regional codes
{prefix}xx-xxx{suffix}
with unicode flags (flag emoji tag sequences)For this method the suffix should not contain A-Z, a-z or 0-9 and not start with a - (minus).
- Parameters:
text (str) โ The text
handle_illegal (callable) โ See flag.flagize()
- Returns:
The text with all occurrences of
{prefix}xx-xxx{suffix}
replaced by unicode flags- Return type:
str
Supported emojis and patterns๏
Complete list can be found here
.
The following flags are supported on all major platforms.
Code |
Emoji |
---|---|
|
๐บ๐ณ |
|
๐ฆ๐จ |
|
๐ฆ๐ฉ |
|
๐ฆ๐ช |
|
๐ฆ๐ซ |
|
๐ฆ๐ฌ |
|
๐ฆ๐ฎ |
|
๐ฆ๐ฑ |
|
๐ฆ๐ฒ |
|
๐ฆ๐ด |
|
๐ฆ๐ถ |
|
๐ฆ๐ท |
|
๐ฆ๐ธ |
|
๐ฆ๐น |
|
๐ฆ๐บ |
|
๐ฆ๐ผ |
|
๐ฆ๐ฝ |
|
๐ฆ๐ฟ |
|
๐ง๐ฆ |
|
๐ง๐ง |
|
๐ง๐ฉ |
|
๐ง๐ช |
|
๐ง๐ซ |
|
๐ง๐ฌ |
|
๐ง๐ญ |
|
๐ง๐ฎ |
|
๐ง๐ฏ |
|
๐ง๐ฑ |
|
๐ง๐ฒ |
|
๐ง๐ณ |
|
๐ง๐ด |
|
๐ง๐ถ |
|
๐ง๐ท |
|
๐ง๐ธ |
|
๐ง๐น |
|
๐ง๐ป |
|
๐ง๐ผ |
|
๐ง๐พ |
|
๐ง๐ฟ |
|
๐จ๐ฆ |
|
๐จ๐จ |
|
๐จ๐ฉ |
|
๐จ๐ซ |
|
๐จ๐ฌ |
|
๐จ๐ญ |
|
๐จ๐ฎ |
|
๐จ๐ฐ |
|
๐จ๐ฑ |
|
๐จ๐ฒ |
|
๐จ๐ณ |
|
๐จ๐ด |
|
๐จ๐ต |
|
๐จ๐ท |
|
๐จ๐บ |
|
๐จ๐ป |
|
๐จ๐ผ |
|
๐จ๐ฝ |
|
๐จ๐พ |
|
๐จ๐ฟ |
|
๐ฉ๐ช |
|
๐ฉ๐ฌ |
|
๐ฉ๐ฏ |
|
๐ฉ๐ฐ |
|
๐ฉ๐ฒ |
|
๐ฉ๐ด |
|
๐ฉ๐ฟ |
|
๐ช๐ฆ |
|
๐ช๐จ |
|
๐ช๐ช |
|
๐ช๐ฌ |
|
๐ช๐ญ |
|
๐ช๐ท |
|
๐ช๐ธ |
|
๐ช๐น |
|
๐ช๐บ |
|
๐ซ๐ฎ |
|
๐ซ๐ฏ |
|
๐ซ๐ฐ |
|
๐ซ๐ฒ |
|
๐ซ๐ด |
|
๐ซ๐ท |
|
๐ฌ๐ฆ |
|
๐ฌ๐ง |
|
๐ฌ๐ฉ |
|
๐ฌ๐ช |
|
๐ฌ๐ซ |
|
๐ฌ๐ฌ |
|
๐ฌ๐ญ |
|
๐ฌ๐ฎ |
|
๐ฌ๐ฑ |
|
๐ฌ๐ฒ |
|
๐ฌ๐ณ |
|
๐ฌ๐ต |
|
๐ฌ๐ถ |
|
๐ฌ๐ท |
|
๐ฌ๐ธ |
|
๐ฌ๐น |
|
๐ฌ๐บ |
|
๐ฌ๐ผ |
|
๐ฌ๐พ |
|
๐ญ๐ฐ |
|
๐ญ๐ฒ |
|
๐ญ๐ณ |
|
๐ญ๐ท |
|
๐ญ๐น |
|
๐ญ๐บ |
|
๐ฎ๐จ |
|
๐ฎ๐ฉ |
|
๐ฎ๐ช |
|
๐ฎ๐ฑ |
|
๐ฎ๐ฒ |
|
๐ฎ๐ณ |
|
๐ฎ๐ด |
|
๐ฎ๐ถ |
|
๐ฎ๐ท |
|
๐ฎ๐ธ |
|
๐ฎ๐น |
|
๐ฏ๐ช |
|
๐ฏ๐ฒ |
|
๐ฏ๐ด |
|
๐ฏ๐ต |
|
๐ฐ๐ช |
|
๐ฐ๐ฌ |
|
๐ฐ๐ญ |
|
๐ฐ๐ฎ |
|
๐ฐ๐ฒ |
|
๐ฐ๐ณ |
|
๐ฐ๐ต |
|
๐ฐ๐ท |
|
๐ฐ๐ผ |
|
๐ฐ๐พ |
|
๐ฐ๐ฟ |
|
๐ฑ๐ฆ |
|
๐ฑ๐ง |
|
๐ฑ๐จ |
|
๐ฑ๐ฎ |
|
๐ฑ๐ฐ |
|
๐ฑ๐ท |
|
๐ฑ๐ธ |
|
๐ฑ๐น |
|
๐ฑ๐บ |
|
๐ฑ๐ป |
|
๐ฑ๐พ |
|
๐ฒ๐ฆ |
|
๐ฒ๐จ |
|
๐ฒ๐ฉ |
|
๐ฒ๐ช |
|
๐ฒ๐ซ |
|
๐ฒ๐ฌ |
|
๐ฒ๐ญ |
|
๐ฒ๐ฐ |
|
๐ฒ๐ฑ |
|
๐ฒ๐ฒ |
|
๐ฒ๐ณ |
|
๐ฒ๐ด |
|
๐ฒ๐ต |
|
๐ฒ๐ถ |
|
๐ฒ๐ท |
|
๐ฒ๐ธ |
|
๐ฒ๐น |
|
๐ฒ๐บ |
|
๐ฒ๐ป |
|
๐ฒ๐ผ |
|
๐ฒ๐ฝ |
|
๐ฒ๐พ |
|
๐ฒ๐ฟ |
|
๐ณ๐ฆ |
|
๐ณ๐จ |
|
๐ณ๐ช |
|
๐ณ๐ซ |
|
๐ณ๐ฌ |
|
๐ณ๐ฎ |
|
๐ณ๐ฑ |
|
๐ณ๐ด |
|
๐ณ๐ต |
|
๐ณ๐ท |
|
๐ณ๐บ |
|
๐ณ๐ฟ |
|
๐ด๐ฒ |
|
๐ต๐ฆ |
|
๐ต๐ช |
|
๐ต๐ซ |
|
๐ต๐ฌ |
|
๐ต๐ญ |
|
๐ต๐ฐ |
|
๐ต๐ฑ |
|
๐ต๐ฒ |
|
๐ต๐ณ |
|
๐ต๐ท |
|
๐ต๐ธ |
|
๐ต๐น |
|
๐ต๐ผ |
|
๐ต๐พ |
|
๐ถ๐ฆ |
|
๐ท๐ช |
|
๐ท๐ด |
|
๐ท๐ธ |
|
๐ท๐บ |
|
๐ท๐ผ |
|
๐ธ๐ฆ |
|
๐ธ๐ง |
|
๐ธ๐จ |
|
๐ธ๐ฉ |
|
๐ธ๐ช |
|
๐ธ๐ฌ |
|
๐ธ๐ญ |
|
๐ธ๐ฎ |
|
๐ธ๐ฏ |
|
๐ธ๐ฐ |
|
๐ธ๐ฑ |
|
๐ธ๐ฒ |
|
๐ธ๐ณ |
|
๐ธ๐ด |
|
๐ธ๐ท |
|
๐ธ๐ธ |
|
๐ธ๐น |
|
๐ธ๐ป |
|
๐ธ๐ฝ |
|
๐ธ๐พ |
|
๐ธ๐ฟ |
|
๐น๐ฆ |
|
๐น๐จ |
|
๐น๐ฉ |
|
๐น๐ซ |
|
๐น๐ฌ |
|
๐น๐ญ |
|
๐น๐ฏ |
|
๐น๐ฐ |
|
๐น๐ฑ |
|
๐น๐ฒ |
|
๐น๐ณ |
|
๐น๐ด |
|
๐น๐ท |
|
๐น๐น |
|
๐น๐ป |
|
๐น๐ผ |
|
๐น๐ฟ |
|
๐บ๐ฆ |
|
๐บ๐ฌ |
|
๐บ๐ฒ |
|
๐บ๐ธ |
|
๐บ๐พ |
|
๐บ๐ฟ |
|
๐ป๐ฆ |
|
๐ป๐จ |
|
๐ป๐ช |
|
๐ป๐ฌ |
|
๐ป๐ฎ |
|
๐ป๐ณ |
|
๐ป๐บ |
|
๐ผ๐ซ |
|
๐ผ๐ธ |
|
๐ฝ๐ฐ |
|
๐พ๐ช |
|
๐พ๐น |
|
๐ฟ๐ฆ |
|
๐ฟ๐ฒ |
|
๐ฟ๐ผ |
Subregional flags๏
The only widely supported subregional flags are currently: England, Scotland and Wales (as of iOS 17 and Android 14).
Code |
Emoji |
---|---|
|
๐ด๓ ง๓ ข๓ ณ๓ ฃ๓ ด๓ ฟ |
|
๐ด๓ ง๓ ข๓ ท๓ ฌ๓ ณ๓ ฟ |
|
๐ด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟ |
|
๐ด๓ ต๓ ณ๓ ด๓ ธ๓ ฟ |
How subregional flags work๏
They work very similar to the country flags. The ASCII codes are transformed by replacing them with specific codepoints that are called โtagsโ.
black_flag_emoji
followed by region_code_in_tag
followed by cancel_tag
Note
black_flag_emoji
:U+1F3F4 ( ๐ด )
cancel_tag
:U+E007F (invisible, signifies the end of the flag code)
region_code_in_tag
:It is formed using the abbreviation defined in ISO 3166-2 and adding 0xE0000 to every ASCII value of the code. For example England is GB-ENG.
A full list of valid codes can be found here: github.com/unicode-org/โฆ/subdivisions/en.xml
Itโs also possible to use a 3-digit-code from github.com/unicode-org/โฆ/UnMacroRegions.txt
Example:๏
GB-ENG
in ISO 3166-2.gbeng
.gbeng
:g
is unicode 0x67 or decimal 103, so 103 + 917504 = 917607 or 0xE0067b
is 0x62 and becomes 0xE0062e
is 0x65 and becomes 0xE0065n
is 0x6E and becomes 0xE006Eg
is 0x67 and becomes 0xE0067Together itโs:
g b e n g
ASCII: 0x67 0x62 0x65 0x6E 0x67
Tags: 0x1F3F4 0xE0067 0xE0062 0xE0065 0xE006E 0xE0067 0xE007F
black_flag cancel_tag
Unlike the regional indicator symbols, tags are not rendered on incompatible system, they will simply be invisible and have no width. So, if the particular flag is not supported or if tag flags are not supported at all, the only visible character will be a black flag.
Validity and Support๏
Unicode publishes a listing of all country codes and subregional codes that are available. The data can be found in the CLDR downloads or in the repository at https://github.com/unicode-org/cldr/blob/release-45/common/validity/region.xml
The module offers this data as a dictionary (roughly 6000 country codes):
import flag
# For a single country code:
flag.info("US")
# {'id_status': 'regular', 'supported': True, 'valid': True}
# List of all country codes
flag.infos(extended=True)
# {'US': {'id_status': 'regular', 'supported': True, 'valid': True}, {'IT': {...}, ...}
The validity (dict-key 'valid'
) is specified in the Unicode Emoji reports:
https://www.unicode.org/reports/tr51/#Flags
The module also offers information about the support of the flags (dict-key 'supported'
). This data is
based on visual inspection on major devices and platforms.
Platforms with full-support are macOS 14, iOS 17, Android 14, Firefox 127, Telegram 10, WhatsApp 2.20.
The data disregards Windows as it does not support any flags by default. Some third-party Windows apps like Firefox or Telegram do support flags.
Also it is disregarded that the Peopleโs Republic of China censors some flags on some devices or on some language/region settings.
You can view the complete list of flags at here
.
Unsupported flags:๏
An Android, Firefox, WhatsApp and Telegram: unsupported two-letter flags are generally displayed as two (white on blue) letters, subregional flags are displayed as black flags.
On Windows two-letter codes are displayed as the letters themselves, subregional codes are displayed as a black flag.
On iOS and macOS unsupported two-letter flags are displayed as two letters with a black rectangle areound the letters, subregional codes are displayed similarly or as a black flag.
Example:๏
import flag
my_flags = flag.Flag(only_supported=True, only_valid=True, allow_subregions=True)
print(my_flags.flagize("Flag of the United States :US:"))
# This will show the US flag instead of :US:
print(my_flags.flagize("A invalid flag :XT:")) # :XT: is kept, as it is not supported
my_flags.flag("XY") # TRaise an error, because XY is not a valid country code
# Add the custom code XT, which is used for the Texas flag by WhatsApp
# And also the standard code US-TX which is understood by WhatsApp (but not generated)
my_flags.add_flag("XT")
my_flags.add_flag("US-TX")
# This will show the Texas flag on WhatsApp
# On other platforms, the first will show the letters ๐ฝ๐น, the second an emmpty flag ๐ด๓ ต๓ ณ๓ ด๓ ธ๓ ฟ
print(my_flags.flagize("The Texas flag :XT:")) p
print(my_flags.flagize("The Texas flag :us-tx:"))