12

Is there any alternatives to the print statement for output in Python.
Also, how can I format my output text to be color-coded?

I'm a beginner programmer, pretty new to it.
Thanks

mjv
  • 73,152
  • 14
  • 113
  • 156
  • Colab and Jupiter note books, allow outputting to markdown. You could also output html, and pipe this into a html renderer. – ctrl-alt-delor Jun 07 '21 at 12:13

4 Answers4

20
sys.stdout.write()

Is one alternative. You can also write to sys.stderr. Personally, I use sys.stdout when I need to pass a generic "stream" to some function that will write to it. And open file is a stream, and sys.stdout is a stream. This way my function can be parametrized to write either to output or to file.

I also find sys.stdout.write more convenient than print for printing many things to a single line, as I find the ending comma syntax of print for suppressing the newline ugly and inconvenient.

Regarding printing to the terminal in colors, see this other SO discussion.

Community
  • 1
  • 1
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
2

You can use the "display()" function instead of print() - I prefer this and it works well to display dataframes

display(dataframe)

or

You can also use Pretty printer : Sample code below

import pprint
stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
stuff.insert(0, stuff[:])
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(stuff)
Naveen Kumar
  • 277
  • 4
  • 5
-1

You can use PrettyPrint for print data

anijhaw
  • 8,954
  • 7
  • 35
  • 36
-1

IceCream — Never use print() to debug again
You should try the package called IceCream To install it, please use the following:

pip install icecream

For details on how to use it, please click here

An example:

from icecream import ic
a = 3
def duplicate(i):
    return i * 2

b = duplicate(ic(a))

ic| a: 6

ic(b)

ic| b: 6

Enjoy!

Yasser M
  • 654
  • 7
  • 9