Search code examples
python-3.xkeyword-argument

Output format in **Kwargs


I want to format my output like- My choice of fruits are apple Avocado. But not sure it's not executing properly, maybe i'm missing something in formatting. Can you let me know where i'm doing it wrong?

 #Kwargs
def my_func(**kwargs):
    print(kwargs)
    if 'fruit' 'veggie' in kwargs:
        print('My choice of fruits are {} {}'.format(kwargs['fruit'],['veggie']))
    else:
        print('Sorry we can not find your fruit here!')

my_func(fruit='apple', veggie= 'Avocado')

Output of the above snippet coming as- {'fruit': 'apple', 'veggie': 'Avocado'} Sorry we can not find your fruit here!


Solution

    1. 'fruit' 'veggie' == 'fruitveggie'
    2. ['veggie'] is a list of str
    3. find out about f-strings

    So,

    def my_func(**kwargs):
        print(kwargs)
        if 'fruit' in kwargs and 'veggie' in kwargs:
            print(f'My choice of fruits are {kwargs["fruit"]} {kwargs["veggie"]}')
        else:
            print('Sorry we can not find your fruit here!')
    
    my_func(fruit='apple', veggie='Avocado')