pip install Pygments
python
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
python
code = """
def hello_world():
print("Hello, World!")
hello_world()
"""
python
formatter = HtmlFormatter(style="default", linenos=True)
python
highlighted_code = highlight(code, PythonLexer(), formatter)
python
print(highlighted_code)
python
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import TerminalFormatter
code = """
def hello_world():
print("Hello, World!")
hello_world()
"""
highlighted_code = highlight(code, PythonLexer(), TerminalFormatter())
print(highlighted_code)