Search code examples
pythonfunctionargs

My function with *args remake only first argument python, but i need them all to be remaked


I need to write a decorator, which delete whitespaces from the start and end of the strigs, which are given like an arguments of another function. At first i tried to write just a function which use strip, but it remakes only first given arg, when i need them all. join needed because without it function returns tuple.

def NewFunc(*strings):
    newstr = ' '.join([str(x) for x in strings])
    return newstr.strip()

print(NewFunc('         Anti   ', '     hype   ', '   ajou!   '))

and it returns: Anti hype ajou!

when i need:Anti hype ajou!

what to change?


Solution

  • strip only removes leading and trailing whitespace, and you are only stripping the final result. You must strip each element before joining them, and this can be done in the list comprehension:

    def NewFunc(*strings):
        newstr = ' '.join([str(x).strip() for x in strings])
        return newstr
    

    The str(x) is a bit unnecessary, but I don't know, maybe you are going to pass in ints or something.