Search code examples
pythonpython-3.xstringstring-formattingpokeapi

How to remove comma at the end of a string


I'm working with an library called pokebase in Python which is a wrapper for Pokeapi.

Code:

import pokebase as pb #pip install pokebase
p1 = pb.pokemon('venusaur')
types = ''
for poketype in p1.types:
    types += poketype.type.name.capitalize()+', '
print(types)

It prints the data like this:

Grass, Poison, 

So I learned how to use this API on a YouTube tutorial, but the problem here is that when i run the for loop it prints ', ' at the end of output also which is grammatically wrong. How can I fix that? I have tried to remove it using end and separator parameters to print() but it doesn't work. How can I remove the comma at the end?


Solution

  • You can do:

    types = ','.join(poketype.type.name.capitalize() for poketype in p1.types)
    

    .join() function takes an iterable and joins its elements.