A custom print function that packs print ()

How can I wrap print()to add arbitrary lines to the beginning and end of what is passed as arguments for printing?

def xprint(*args):
    print("XXX", *args, "XXX")
xprint("hi", "yo", 4)

does not work

Essentially, I want my custom function to xprint()work as print()it does, but add 'XXX'to the beginning and end of each output.

+7
source share
3 answers

Will work for python 2 and 3 if there are no keyword arguments

def xprint(*args):
    print( "XXX"+" ".join(map(str,args))+"XXX")

In [5]: xprint("hi", "yo", 4)
XXXhi yo 4XXX

For a python 3 function print()(or when used print_functionfrom __future__in python 2), keyword arguments may also be present. To ensure their transfer, use the form

def xprint(*args, **kwargs):
    print( "XXX"+" ".join(map(str,args))+"XXX", **kwargs)
+13

args

def xprint(*args):
    for i in args:
        print('XXX{}XXX'.format(i), end=' ')

>>> xprint('hi', 'yo', 4)
XXXhiXXX XXXyoXXX XXX4XXX 

XXX,

def xprint(*args):
    s = ' '.join(str(i) for i in args)
    print('XXX{}XXX'.format(s), end=' ')

>>> xprint('hi', 'yo', 4)
XXXhi yo 4XXX 
0

:

def xprint(*args):
    args = ("XXX",)+args+("XXX",)
    print(*args)
xprint("hi", "yo", 4)

XXX hi yo 4 XXX

0

All Articles