Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple args in custom print

I define debug_print():

def debug_print(info):
  print("[FOO] :",info)

I expect I can call like normal print like this:

name = 'Ujang'
debug_print(name, 'is winner!'); # I expect [FOO] : Ujang is winner!

But it gave me error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: debug_print() takes 1 positional argument but 2 were given

I tried redefine debug_print parameter with using pointer symbol
def debug_print(*info) but it’s showing tuple format.

# output
[FOO] : ('Ujang', 'is winner!')

>Solution :

You can use join() function without map() as follows:

def debug_print(*info):
  print("[FOO] :", ' '.join(info))
  
name = 'Ujang'
debug_print(name, 'is winner!')

Output:

[FOO] : Ujang is a winner!

Also, as @KellyBundy suggested, you can take advantage of the print() function and pass *info as a parameter, so the print function will automatically convert all of its values to string:

def debug_print(*info):
  print("[FOO] :", *info)
  
name = 'Ujang'
debug_print(name, 'is winner!')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading