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)
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.
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
.
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.
aayoubi
Dies hängt davon ab, ob Sie verwenden Linux oder Unix:
>>> start = "\033[1m"
>>> end = "\033[0;0m"
>>> print "The" + start + "text" + end + " is bold."
The text is bold.
The word text
should be bold.
Re “using Linux”: Why is that? ANSI escape sequences? Can you elaborate in your answer? What are the preconditions for it to work (e.g., terminal emulation support)? – where doesn’t it work? But ********* without ********* “Edit:”, “Update:”, or similar – the answer should appear as if it was written today)
Nov 2 at 18:11
Peter Mortensen
There is a very useful module for formatting text (bold, underline, colors, etc.) in Python. It uses the curses library, but it’s very straightforward 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")
Re “using Linux”: Why is that? ANSI escape sequences? Can you elaborate in your answer? What are the preconditions for it to work (e.g., terminal emulation support)? – where doesn’t it work? But ********* without ********* “Edit:”, “Update:”, or similar – the answer should appear as if it was written today)
Nov 2 at 18:11
Peter Mortensen
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
Safari kann implizieren Mac OS X v10.7 (Löwe).
– Peter Mortensen
2. November um 18:43 Uhr