简体   繁体   中英

How to print list inside python print?

I am trying to build a variable number of arguments logger as follow. I would like to call it like:

log( 1, "Index: ", request_index, ", ", section )

def log(level, *msg) :

    global print_debug_lastTime
    currentTime = datetime.datetime.now().microsecond

    # You can access global variables without the global keyword.
    if g_debug_level & level != 0:

        print( "[DEBUG] " \
                + "%02d" % datetime.datetime.now().hour + ":" \
                + "%02d" % datetime.datetime.now().minute + ":" \
                + "%02d" % datetime.datetime.now().second + ":" \
                + str( currentTime ) \
                + "%7d " % ( currentTime - print_debug_lastTime ) \
                + for m in msg )

But I am having trouble to print the variable number of arguments. Initially I am trying to run this simple code on the interpreter:

>>> print( str( x ) for x in (0,1,2,3) )
<generator object <genexpr> at 0x6ffffe5e550>

I would like it to print 0123 , but it is printing it as may be seem.

Try this:

print(''.join(str(x) for x in [0, 1, 2, 3]))
# 0123

enclose your for with "[]"

print(tuple([str( x ) for x in (0,1,2,3)]))

output = ['0', '1', '2', '3']

convert to tuple if you want

print(tuple([str( x ) for x in (0,1,2,3)]))

output = ('0', '1', '2', '3')

AND in your case you want to concatenate with other string you can use following and add in your log string,

tuple(str( x ) for x in (0,1,2,3)).__str__()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM