Country Name
name_to_alpha2
takes in a string and searches for a corresponding alpha-2 code in the database for both common and official country names in 34 different languages. If no match is found, None
is returned.
The function includes a use_fuzzy=True
option, which enables fuzzy lookup to match various name variations such as Germany Federal Republic of
and minor misspellings such as Swizerland
. Although using fuzzy lookup may incur a significant performance cost of approximately 100x slower than the normal lookup, it is capable of capturing virtually all variations of country names. It is recommended to enable the fuzzy option whenever speed is not a critical factor, as it typically takes around 50ms to return a result on an average desktop computer.
Usage Example
Basic Usage | |
---|---|
Supported Languages
Name | Code |
---|---|
Arabic | ar |
Armenian | hy |
Basque | eu |
Bulgarian | bg |
Chinese (simplified) | zh |
Chinese (traditional) | zh-tw |
Croatian | hr |
Czech | cs |
Danish | da |
Dutch | nl |
English | en |
Esperanto | eo |
Estonian | et |
Finnish | fi |
French | fr |
German | de |
Greek | el |
Hungarian | hu |
Italian | it |
Japanese | ja |
Korean | ko |
Lithuanian | lt |
Norwegian | no |
Polish | pl |
Portuguese | pt |
Romanian | ro |
Russian | ru |
Serbian | sr |
Slovak | sk |
Slovenian | sl |
Spanish | es |
Swedish | sv |
Thai | th |
Ukrainian | uk |