[Python] Color only a part of a line in the output

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have the below script which works fine, I only want to make the The running sum is: integer output to be colored.

    I have defined the class style which I am trying to use.

    Code:

    #!/usr/local/bin/python3.6
    import os
    os.system("")

    # Group of Different functions for different styles
    class style():
    BLACK = '\033[30m'
    RED = '\033[31m'
    GREEN = '\033[32m'
    YELLOW = '\033[33m'
    BLUE = '\033[34m'
    MAGENTA = '\033[35m'
    CYAN = '\033[36m'
    WHITE = '\033[37m'
    UNDERLINE = '\033[4m'
    RESET = '\033[0m'

    def running_sum(n):
    running_sum = 0
    for k in range(n):
    running_sum += k
    print(f"The running sum is: {running_sum}")

    if __name__ == "__main__":
    running_sum(int(input("Enter an integer: ")))


    Script output:

    Enter an integer: 15
    The running sum is: 105


    In the above script 105 is the output which I want to printed in red color.

    I tried to use

    print(style.RED + f"The running sum is: {running_sum}")


    but this makes the entire output red.

    Continue reading...

Compartilhe esta Página