Home > Net >  Print string without single quotes & brackets
Print string without single quotes & brackets

Time:10-07

After installing countryinfo and import CountryInfo, I have tried to print

currency = country.currencies()
print(currency)

and the result is ['CAD']. I am interested in CAD without quotes & brackets.

CodePudding user response:

It sounds like country.currencies() returns a list, which is why you see ['CAD']. In order to access the first (and only) element in the list, which is a string, you can use currency[0]. To print it, you can use print(currency[0]), which will give the desired output.

I would strongly suggest reading about lists, what you can put inside of them, and most importantly, how you can access elements in them.

CodePudding user response:

You need to get element of the list by doing this:

print(currency[0])

CodePudding user response:

According to the countryinfo documentation, the .countries() function returns an array of strings:

# coding=utf-8
from countryinfo import CountryInfo


country = CountryInfo('Singapore')
country.currencies()
# returns array of strings, currencies
# ['SGD']

You just need to print a specific entry from an array, in your case it would be: print(currency[0]).

  • Related