Z.B:
print "hello"
Was soll ich tun, um den Text “Hallo” fett zu machen?
Jia-Luo
Z.B:
print "hello"
Was soll ich tun, um den Text “Hallo” fett zu machen?
Bouba
Klassenfarbe: LILA = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
print(color.BOLD + 'Hello World !' + color.END)
I like the way you didn’t just did bold, but created a whole class for them to reference and to help all users viewing. Thank you.
Dec 9, 2016 at 21:43
I’m gonna use this as dict, not a class. Thanks!
Feb 27, 2017 at 20:49
It is printing the following : [1mHello World ![0m
Jul 12, 2017 at 17:59
Can I combine bold with color as well? @Boubakr
Apr 12, 2020 at 12:29
Yes, just concatenate the two vars: print(color.RED + color.BOLD + "Bold red string" + color.END)
. (BTW, works with Python 3.8.2 (at least on Ubuntu 20.04).)
May 18, 2020 at 10:13
Addison
Use this:
print '\033[1m' + 'Hello'
And to change back to normal:
print '\033[0m'
This page is a good reference for printing in colors and font-weights. Go to the section that says ‘Set graphics mode:’
And note this won’t work on all operating systems but you don’t need any modules.
To all Windows users, note: This is not working on Windows (Windows 10)
May 8, 2021 at 14:12
For me it works on Windows 10 with print('\033[1m' + 'Hello')
.
Jan 5 at 14:29
Thank you. I want to report you that the link doesn’t work.
Feb 1 at 7:33
@EarlMascetti Thanks for letting me know! I just updated the link to use the wayback machine
Feb 7 at 20:21
Zuko
You can use termcolor for this:
sudo pip install termcolor
To print a colored bold:
from termcolor import colored
print(colored('Hello', 'green', attrs=['bold']))
Weitere Informationen finden Sie unter termcolor auf PyPi.
einfache Farben ist ein weiteres Paket mit ähnlicher Syntax:
from simple_colors import *
print(green('Hello', ['bold'])
Das Äquivalent in colorama vielleicht Style.BRIGHT
.
Wenn ich raten müsste, dass jemand dies abgelehnt hat, weil es die eigentliche Frage, wie etwas fett gedruckt wird, nicht beantwortet hat.
– Christian
10. Oktober 2019 um 10:54 Uhr
Du hast Recht, es gab ein Update. Tut mir leid, Jungs.
– Zuko
25. Mai 2020 um 6:30 Uhr
In der reinen Computerprogrammierung gibt es so etwas wie “fett gedruckten Text” nicht. Lassen Sie uns ein wenig zurückgehen und verstehen, dass Ihr Text eine Zeichenfolge von Bytes ist und Bytes nur Bündel von Bits sind. An den Computer, hier ist Ihr „Hallo“-Text binär.
0110100001100101011011000110110001101111
Jede Eins oder Null ist ein Bit. Alle acht Bits sind ein Byte. Jedes Byte ist in einem String wie dem in Python 2.x ein Buchstabe/eine Zahl/ein Satzzeichen (als Zeichen bezeichnet). Also zum Beispiel:
01101000 01100101 01101100 01101100 01101111
h e l l o
Der Computer übersetzt diese Bits in Buchstaben, aber in einer herkömmlichen Zeichenfolge (als ASCII-Zeichenfolge bezeichnet) gibt es nichts, was auf fetten Text hindeutet. In einer Unicode-Zeichenfolge, die etwas anders funktioniert, kann der Computer internationale Sprachzeichen wie chinesische unterstützen, aber auch hier gibt es nichts zu sagen, dass manche Texte fett sind und manche nicht. Es gibt auch keine explizite Schriftart, Textgröße usw.
Beim Drucken von HTML geben Sie immer noch einen String aus. Aber das Computerprogramm, das diese Zeichenfolge liest (ein Webbrowser), ist so programmiert, dass es Text wie folgt interpretiert this is <b>bold</b>
als „das ist Fett gedruckt“, wenn es Ihre Buchstabenfolge in Pixel auf dem Bildschirm umwandelt. Wenn der gesamte Text WYSIWYG wäre, würde die Notwendigkeit für HTML selbst gemildert – Sie würden einfach Text in Ihrem Editor auswählen und ihn fett formatieren, anstatt den HTML-Code einzutippen.
Andere Programme verwenden andere Systeme – viele Antworten erklärten ein völlig anderes System zum Drucken von fettem Text auf Terminals. Ich bin froh, dass Sie herausgefunden haben, wie Sie das tun können, was Sie tun möchten, aber irgendwann werden Sie verstehen wollen, wie Zeichenfolgen und Speicher funktionieren.
Dies hängt davon ab, ob Sie Linux/Unix verwenden:
>>> start = "\033[1m"
>>> end = "\033[0;0m"
>>> print "The" + start + "text" + end + " is bold."
The text is bold.
The word text
should be bold.
There is a very useful module for formatting text (bold, underline, colors..) in Python. It uses curses
lib but it’s very straight-forward to use.
An example:
from terminal import render
print render('%(BG_YELLOW)s%(RED)s%(BOLD)sHey this is a test%(NORMAL)s')
print render('%(BG_GREEN)s%(RED)s%(UNDERLINE)sAnother test%(NORMAL)s')
I wrote a simple module named colors.py to make this a little more pythonic:
import colors
with colors.pretty_output(colors.BOLD, colors.FG_RED) as out:
out.write("This is a bold red text")
with colors.pretty_output(colors.BG_GREEN) as out:
out.write("This output have a green background but you " +
colors.BOLD + colors.FG_RED + "can" + colors.END + " mix styles")
John Szakmeister
Check out colorama. It doesn’t necessarily help with bolding… but you can do colorized output on both Windows and Linux, and control the brightness:
from colorama import *
init(autoreset=True)
print Fore.RED + 'some red text'
print Style.BRIGHT + Fore.RED + 'some bright red text'
Duplikat von Farbtext in Terminalanwendungen in Unix. Viele Links in den Antworten. Diese Antwort ist in C, aber leicht in Python zu übersetzen.
– Jo
19. Januar 2012 um 10:07 Uhr
Welches Endgerät verwendest du? Arbeiten Sie unter Unix oder Windows?
– Sjoerd
19. Januar 2012 um 10:08 Uhr
Ich benutze Safari. Ich habe gerade herausgefunden, dass ich HTML-Tags in Python verwenden kann.
– Jia-Luo
19. Januar 2012 um 11:01 Uhr