-1

I have a string that I am dynamically creating based off user input. I am using the .format function in Python to add a list into the string, but I would like to remove the quotes and brackets when printing.

I have tried:

return (('{} is {}x effective against {}').format(opponentType, overallHitMultiplier, [str(x) for x in playerTypes]))

and

return return (('{} is {}x effective against {}').format(opponentType, overallHitMultiplier, playerTypes))

which both return a string that looks like this:

fighting is 2x effective against ['normal', 'ghost']

but I would like it to return something like:

fighting is 2x effective against normal, ghost

The length of the list is variable so I can't just insert the list elements one by one.

Harry Lees
  • 41
  • 5

1 Answers1

2

Here is a more complete response:

def convert_player_types_to_str(player_types):
    n = len(player_types)
    if not n:
        return ''
    if n == 1:
        return player_types[0]
    return ', '.join(player_types[:-1]) + f' and {player_types[-1]}'

>>> convert_player_types_to_str(['normal'])
'normal'

>>> convert_player_types_to_str(['normal', 'ghost'])
'normal and ghost'

>>> convert_player_types_to_str(['normal', 'ghost', 'goblin'])
'normal, ghost and goblin'
Alexander
  • 105,104
  • 32
  • 201
  • 196