Custom print function that wraps print()

12,900

Solution 1

Will work for python 2 and 3 when 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 the python 3 print() function (or when using print_function from __future__ in python 2), keyword arguments may be present as well. To ensure these are passed use the form

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

Solution 2

You can do the same thing without changing print function name. Just add below code in your script.

xprint = print


def print(*args, **kwargs):
    # do whatever you want to do
    xprint('statement before print')
    xprint(*args, **kwargs)


print(f'hello')
Share:
12,900
user2316370
Author by

user2316370

Updated on June 05, 2022

Comments

  • user2316370
    user2316370 about 2 years

    How can I wrap print() so that I can add arbitrary strings to the beginning and end of the things that are passed as arguments to get printed?

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

    doesn't work.

    Basically, I want my custom function xprint() to work like print() but add 'XXX' to the beginning and end of every output.

  • wjandrea
    wjandrea about 7 years
    For Python3, it should be def xprint(*args, **kwargs): ..., no?
  • MartyMacGyver
    MartyMacGyver about 7 years
    @wjandrea - Good point! I've edited this with the clarification.
  • Ronen Ness
    Ronen Ness almost 7 years
    on python 2.x this gives invalid syntax.
  • Ronen Ness
    Ronen Ness almost 7 years
    Overall a good solution but note that this won't support non ascii unicode + this don't support print ending with comma to stay on the same line.
  • Padraic Cunningham
    Padraic Cunningham almost 7 years
    @RonenNess, what error are you getting with non-ascii, also py3 or py2? If it is py2, you should either be prepending the string with a u/decoding etc.. For py3 you should have very little issue bar your shell limitations like you enjoy on windows, you can also implement the sep arg in py3 or import the print_function in py2, there is no limitation to what you want to add it terms of how print works.
  • Ronen Ness
    Ronen Ness almost 7 years
    @Padraic Cunningham I'm talking about Python 2, ofc, where all unicode / str problems reside :) Anyway I must correct myself its something that shouldn't work with regular print either, but with IDLE it does because IDLE works a little differently with print. With that said, consider the following input: print(u'\u05d1\u05d3\u05d9\u05e7\u05d4') - it works with print, but throw exception with xprint. Anyway this is in IDLE, in shell it throws exception in both print and xprint (encoding is needed), so this is a non issue imo.
  • wjakobw
    wjakobw over 6 years
    If you want the sep-keyword to work, do this: def xprint(*args, sep=" ", **kwargs): print("XXX"+sep.join(map(str,args))+"XXX", **kwargs)`