{"id":10272,"library":"style","title":"Terminal String Styling","description":"Style is a simple and fast Python library for styling terminal output with colors and formatting using ANSI escape codes. It provides a clean API for foreground colors, background colors, and text effects. The current version is 1.1.6, and it maintains a stable, active release cadence.","status":"active","version":"1.1.6","language":"en","source_language":"en","source_url":"https://github.com/lmittmann/style","tags":["terminal","styling","colors","cli","ansi"],"install":[{"cmd":"pip install style","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"style, fg, bg, ef","correct":"from style import style, fg, bg, ef"}],"quickstart":{"code":"from style import style, fg, bg, ef\n\nprint(style(\"Hello, World!\", fg.red, bg.blue, ef.bold))\nprint(style(\"Underlined text.\", ef.underline))\nprint(style(\"Green background.\", bg.green))","lang":"python","description":"This example demonstrates basic usage of `style` to apply foreground color, background color, and text effects like bold or underline to a string. The `style` function returns the styled string, which can then be printed."},"warnings":[{"fix":"Apply styles to individual components and concatenate them. For example, `print(style('Hello ', fg.red) + style('World!', fg.blue))` for distinct styling, rather than `print(style('Hello ' + style('World!', fg.blue), fg.red))` which won't apply red to 'World!'.","message":"When applying styles using the `style()` function, inner `style()` calls effectively create new styled segments that are concatenated. This means that a parent style will not 'wrap' a child styled segment as it implicitly resets before and after each `style()` call.","severity":"gotcha","affected_versions":"All versions (1.0.0+)"},{"fix":"Always use lowercase attribute names for colors and effects as defined in the `fg`, `bg`, and `ef` objects.","message":"Styles (colors and effects) are applied by their lowercase attribute names (e.g., `fg.red`, `ef.bold`). Using uppercase attribute names (e.g., `fg.RED`) will result in an `AttributeError`.","severity":"gotcha","affected_versions":"All versions (1.0.0+)"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure you import the necessary components: `from style import style, fg, bg, ef`.","cause":"Attempting to use `fg` (or `bg`, `ef`, `style`) without importing it from the `style` library.","error":"NameError: name 'fg' is not defined"},{"fix":"Change the attribute name to its lowercase equivalent, for example, `fg.red`, `bg.blue`, or `ef.bold`.","cause":"You are trying to access a color or effect attribute using uppercase letters (e.g., `fg.RED`) instead of the correct lowercase form.","error":"AttributeError: module 'style.fg' has no attribute 'RED'"},{"fix":"Ensure you are using a modern terminal emulator (e.g., Windows Terminal, iTerm2, Alacritty, gnome-terminal). If on Windows, ensure `colorama` is installed (`pip install colorama`) as `style` relies on it for cross-platform compatibility. Avoid redirecting output to files if you expect styled text.","cause":"The terminal emulator you are using may not support ANSI escape codes, or the output is being redirected (e.g., to a file) which often disables styling. On older Windows versions, `colorama` (which `style` uses) might not be fully initialized or the terminal itself lacks sufficient support.","error":"My terminal output is not colored or styled, just plain text or strange characters."}]}