14 Replies - 1206 Views - Last Post: 25 December 2015 - 12:44 PM Rate Topic: -----

#1 CrazySynthax  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 12-June 15

Print some text to the console in BOLD

Posted 24 December 2015 - 07:51 AM

Hi Guys,

Do you know if there is any way to print BOLD text to the console in Python?
Do you know how to do it?
Is This A Good Question/Topic? 0
  • +

Replies To: Print some text to the console in BOLD

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13558
  • View blog
  • Posts: 54,106
  • Joined: 12-June 08

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 08:20 AM

That is typically console/platform dependent.
Was This Post Helpful? 0
  • +
  • -

#3 CrazySynthax  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 12-June 15

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 08:23 AM

My OS is Windows. Do you need some more info?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13558
  • View blog
  • Posts: 54,106
  • Joined: 12-June 08

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 08:32 AM

Have you looked at searching "windows console bold python"?
Was This Post Helpful? 0
  • +
  • -

#5 CrazySynthax  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 12-June 15

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 03:03 PM

Yes. I did and I saw that I have to write:
print '\033[1m' + 'Hello'

in order to get a bold "hello".

I did it, and I didn't get anything bold.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,892
  • Joined: 12-December 12

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 03:10 PM

That works for me, on Windows and Python 3. How are you running it?

I suppose you could try changing back to normal text:
print('\033[1m' + 'Hello' + '\033[0m')

Was This Post Helpful? 0
  • +
  • -

#7 CrazySynthax  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 12-June 15

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 03:11 PM

But I'm using Python 2.7. Do you have something for this version?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,892
  • Joined: 12-December 12

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 03:18 PM

One wouldn't have thought that it would matter, as they are ANSI codes, but maybe it does, and you could try importing to utilise the Python 3 print() function, described here.
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2342
  • View blog
  • Posts: 9,393
  • Joined: 03-December 12

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 03:26 PM

Which console are you refering to? IDLE has a terminal, which may not render it. The windows command console should however.
Was This Post Helpful? 0
  • +
  • -

#10 CrazySynthax  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 12-June 15

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 03:33 PM

I use Windows console.

I open a python file and write:
print('\033[1m' + 'Hello' + '\033[0m') 


Then, I execute the script from command window by writing:
python test.py


and I get:

Quote

←[1mHello←[0m

Was This Post Helpful? 0
  • +
  • -

#11 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2342
  • View blog
  • Posts: 9,393
  • Joined: 03-December 12

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 03:52 PM

Hmm I am getting the same thing.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,892
  • Joined: 12-December 12

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 03:59 PM

Writing print() (with parentheses) isn't using the Python 3 print function, you have to import it from __future__. See my previous post.
Was This Post Helpful? 0
  • +
  • -

#13 CrazySynthax  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 12-June 15

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 04:43 PM

Yes, but it seems that __future__ is an external module. I can't use it. I can use only "pure" Python 2.7
Was This Post Helpful? 0
  • +
  • -

#14 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,583
  • Joined: 13-June 14

Re: Print some text to the console in BOLD

Posted 24 December 2015 - 11:48 PM

I'm not sure what you mean by "external module" - __future__ is part of the standard library (see the docs, specifically section 28, Python Runtime Services). Having said that, I'd find it weird if the print() function somehow honoured the ANSI escape codes, but the print statement didn't. I'd think it has more to do with the underlying console. I'm not a Windows user, so can't comment on that (the ANSI escape sequences work for me in my Linux console, but I imagine that's not much help..).

This post has been edited by ndc85430: 24 December 2015 - 11:49 PM

Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6996
  • View blog
  • Posts: 14,634
  • Joined: 16-October 07

Re: Print some text to the console in BOLD

Posted 25 December 2015 - 12:44 PM

View Postndc85430, on 25 December 2015 - 01:48 AM, said:

I'd think it has more to do with the underlying console.


Agreed, not all Windows/DOS consoles have ANSI by default. In the ancient days of DOS, ansi.sys need be loaded. While none of those systems should be live today, well, people still use some really old stuff.

If it matters, I'd look at those "external" libraries. There's python binding for almost everything, curses, conio(!?!), a number of terminal libs.

Curiously, this one notes that bold doesn't work in Windows: https://pypi.python.org/pypi/termcolor
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1