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.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
- class flag.Flag(prefix_str: str = ':', suffix_str: str = ':', warn: bool = True)[source]๏
Use this class if you want a different prefix and suffix instead of colons. Offers the same methods as the module.
- __init__(prefix_str: str = ':', suffix_str: str = ':', 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
- 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
{prefix}XX{suffix}
- Parameters:
text (str) โ The text
subregions (bool) โ Also replace subregional/subdivision flags (flag emoji tag sequences) with
{prefix}xx-xxx{suffix}
- Returns:
The text with all unicode flags replaced by ascii sequence
{prefix}XX{suffix}
- Return type:
str
- 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
{prefix}xx-xxx{suffix}
- Parameters:
text (str) โ The text
- Returns:
The text with all regional flags replaced by ascii sequence
{prefix}xx-xxx{suffix}
- Return type:
str
- flagize(text: str, subregions: bool = False) 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).
- Parameters:
text (str) โ The text
subregions (bool) โ Also replace subregional/subdivision codes
{prefix}xx-xxx{suffix}
with unicode flags (flag emoji tag sequences).
- Returns:
The text with all occurrences of
{prefix}XX{suffix}
replaced by unicode flags- Return type:
str
- flagize_subregional(text: str) 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
- Returns:
The text with all occurrences of
{prefix}xx-xxx{suffix}
replaced by unicode flags- Return type:
str
Supported emojis and patterns๏
(List may be incomplete)
Code |
Emoji |
---|---|
|
๐บ๐ณ |
|
๐ฆ๐จ |
|
๐ฆ๐ฉ |
|
๐ฆ๐ช |
|
๐ฆ๐ซ |
|
๐ฆ๐ฌ |
|
๐ฆ๐ฎ |
|
๐ฆ๐ฑ |
|
๐ฆ๐ฒ |
|
๐ฆ๐ด |
|
๐ฆ๐ถ |
|
๐ฆ๐ท |
|
๐ฆ๐ธ |
|
๐ฆ๐น |
|
๐ฆ๐บ |
|
๐ฆ๐ผ |
|
๐ฆ๐ฝ |
|
๐ฆ๐ฟ |
|
๐ง๐ฆ |
|
๐ง๐ง |
|
๐ง๐ฉ |
|
๐ง๐ช |
|
๐ง๐ซ |
|
๐ง๐ฌ |
|
๐ง๐ญ |
|
๐ง๐ฎ |
|
๐ง๐ฏ |
|
๐ง๐ฑ |
|
๐ง๐ฒ |
|
๐ง๐ณ |
|
๐ง๐ด |
|
๐ง๐ถ |
|
๐ง๐ท |
|
๐ง๐ธ |
|
๐ง๐น |
|
๐ง๐ป |
|
๐ง๐ผ |
|
๐ง๐พ |
|
๐ง๐ฟ |
|
๐จ๐ฆ |
|
๐จ๐จ |
|
๐จ๐ฉ |
|
๐จ๐ซ |
|
๐จ๐ฌ |
|
๐จ๐ญ |
|
๐จ๐ฎ |
|
๐จ๐ฐ |
|
๐จ๐ฑ |
|
๐จ๐ฒ |
|
๐จ๐ณ |
|
๐จ๐ด |
|
๐จ๐ต |
|
๐จ๐ท |
|
๐จ๐บ |
|
๐จ๐ป |
|
๐จ๐ผ |
|
๐จ๐ฝ |
|
๐จ๐พ |
|
๐จ๐ฟ |
|
๐ฉ๐ช |
|
๐ฉ๐ฌ |
|
๐ฉ๐ฏ |
|
๐ฉ๐ฐ |
|
๐ฉ๐ฒ |
|
๐ฉ๐ด |
|
๐ฉ๐ฟ |
|
๐ช๐ฆ |
|
๐ช๐จ |
|
๐ช๐ช |
|
๐ช๐ฌ |
|
๐ช๐ญ |
|
๐ช๐ท |
|
๐ช๐ธ |
|
๐ช๐น |
|
๐ช๐บ |
|
๐ซ๐ฎ |
|
๐ซ๐ฏ |
|
๐ซ๐ฐ |
|
๐ซ๐ฒ |
|
๐ซ๐ด |
|
๐ซ๐ท |
|
๐ฌ๐ฆ |
|
๐ฌ๐ง |
|
๐ฌ๐ฉ |
|
๐ฌ๐ช |
|
๐ฌ๐ซ |
|
๐ฌ๐ฌ |
|
๐ฌ๐ญ |
|
๐ฌ๐ฎ |
|
๐ฌ๐ฑ |
|
๐ฌ๐ฒ |
|
๐ฌ๐ณ |
|
๐ฌ๐ต |
|
๐ฌ๐ถ |
|
๐ฌ๐ท |
|
๐ฌ๐ธ |
|
๐ฌ๐น |
|
๐ฌ๐บ |
|
๐ฌ๐ผ |
|
๐ฌ๐พ |
|
๐ญ๐ฐ |
|
๐ญ๐ฒ |
|
๐ญ๐ณ |
|
๐ญ๐ท |
|
๐ญ๐น |
|
๐ญ๐บ |
|
๐ฎ๐จ |
|
๐ฎ๐ฉ |
|
๐ฎ๐ช |
|
๐ฎ๐ฑ |
|
๐ฎ๐ฒ |
|
๐ฎ๐ณ |
|
๐ฎ๐ด |
|
๐ฎ๐ถ |
|
๐ฎ๐ท |
|
๐ฎ๐ธ |
|
๐ฎ๐น |
|
๐ฏ๐ช |
|
๐ฏ๐ฒ |
|
๐ฏ๐ด |
|
๐ฏ๐ต |
|
๐ฐ๐ช |
|
๐ฐ๐ฌ |
|
๐ฐ๐ญ |
|
๐ฐ๐ฎ |
|
๐ฐ๐ฒ |
|
๐ฐ๐ณ |
|
๐ฐ๐ต |
|
๐ฐ๐ท |
|
๐ฐ๐ผ |
|
๐ฐ๐พ |
|
๐ฐ๐ฟ |
|
๐ฑ๐ฆ |
|
๐ฑ๐ง |
|
๐ฑ๐จ |
|
๐ฑ๐ฎ |
|
๐ฑ๐ฐ |
|
๐ฑ๐ท |
|
๐ฑ๐ธ |
|
๐ฑ๐น |
|
๐ฑ๐บ |
|
๐ฑ๐ป |
|
๐ฑ๐พ |
|
๐ฒ๐ฆ |
|
๐ฒ๐จ |
|
๐ฒ๐ฉ |
|
๐ฒ๐ช |
|
๐ฒ๐ซ |
|
๐ฒ๐ฌ |
|
๐ฒ๐ญ |
|
๐ฒ๐ฐ |
|
๐ฒ๐ฑ |
|
๐ฒ๐ฒ |
|
๐ฒ๐ณ |
|
๐ฒ๐ด |
|
๐ฒ๐ต |
|
๐ฒ๐ถ |
|
๐ฒ๐ท |
|
๐ฒ๐ธ |
|
๐ฒ๐น |
|
๐ฒ๐บ |
|
๐ฒ๐ป |
|
๐ฒ๐ผ |
|
๐ฒ๐ฝ |
|
๐ฒ๐พ |
|
๐ฒ๐ฟ |
|
๐ณ๐ฆ |
|
๐ณ๐จ |
|
๐ณ๐ช |
|
๐ณ๐ซ |
|
๐ณ๐ฌ |
|
๐ณ๐ฎ |
|
๐ณ๐ฑ |
|
๐ณ๐ด |
|
๐ณ๐ต |
|
๐ณ๐ท |
|
๐ณ๐บ |
|
๐ณ๐ฟ |
|
๐ด๐ฒ |
|
๐ต๐ฆ |
|
๐ต๐ช |
|
๐ต๐ซ |
|
๐ต๐ฌ |
|
๐ต๐ญ |
|
๐ต๐ฐ |
|
๐ต๐ฑ |
|
๐ต๐ฒ |
|
๐ต๐ณ |
|
๐ต๐ท |
|
๐ต๐ธ |
|
๐ต๐น |
|
๐ต๐ผ |
|
๐ต๐พ |
|
๐ถ๐ฆ |
|
๐ท๐ช |
|
๐ท๐ด |
|
๐ท๐ธ |
|
๐ท๐บ |
|
๐ท๐ผ |
|
๐ธ๐ฆ |
|
๐ธ๐ง |
|
๐ธ๐จ |
|
๐ธ๐ฉ |
|
๐ธ๐ช |
|
๐ธ๐ฌ |
|
๐ธ๐ญ |
|
๐ธ๐ฎ |
|
๐ธ๐ฏ |
|
๐ธ๐ฐ |
|
๐ธ๐ฑ |
|
๐ธ๐ฒ |
|
๐ธ๐ณ |
|
๐ธ๐ด |
|
๐ธ๐ท |
|
๐ธ๐ธ |
|
๐ธ๐น |
|
๐ธ๐ป |
|
๐ธ๐ฝ |
|
๐ธ๐พ |
|
๐ธ๐ฟ |
|
๐น๐ฆ |
|
๐น๐จ |
|
๐น๐ฉ |
|
๐น๐ซ |
|
๐น๐ฌ |
|
๐น๐ญ |
|
๐น๐ฏ |
|
๐น๐ฐ |
|
๐น๐ฑ |
|
๐น๐ฒ |
|
๐น๐ณ |
|
๐น๐ด |
|
๐น๐ท |
|
๐น๐น |
|
๐น๐ป |
|
๐น๐ผ |
|
๐น๐ฟ |
|
๐บ๐ฆ |
|
๐บ๐ฌ |
|
๐บ๐ฒ |
|
๐บ๐ธ |
|
๐บ๐พ |
|
๐บ๐ฟ |
|
๐ป๐ฆ |
|
๐ป๐จ |
|
๐ป๐ช |
|
๐ป๐ฌ |
|
๐ป๐ฎ |
|
๐ป๐ณ |
|
๐ป๐บ |
|
๐ผ๐ซ |
|
๐ผ๐ธ |
|
๐ฝ๐ฐ |
|
๐พ๐ช |
|
๐พ๐น |
|
๐ฟ๐ฆ |
|
๐ฟ๐ฒ |
|
๐ฟ๐ผ |
Subregional flags๏
The only widely supported subregional flags are currently: England, Scotland and Wales (as of iOS 12 and Android 9).
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.