'ANSI escape sequence "\033[1m" printing bold for rest of program instead of specified line
I'm using the ANSI escape sequence for bold text;"\033[1m" in a print statement to create a bold header for my Python project. The problem is once I write a line like this: print("\033[1m"+"title"), any normal print statement I write afterword returns in bold as well. How can I make just one specified line bold instead of everything? Code:
print("\033[1m"+"Title")
print("Why is this text in bold as well?")
Solution 1:[1]
In fact, any code we type into this prompt will also be colored bold, as will any subsequent output! That is how Ansi colors work: once you print out the special code enabling a color, the color persists forever until someone else prints out the code for a different color, or prints out the Reset code to disable it.
We can disable it by printing the Reset code above:
print("\033[1m"+"Title"+"\033[0m")
Solution 2:[2]
You need to end the escape sequence. Using \033[0m will reset all formatting.
print("\033[1m"+"Title"+"\033[0m")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Python learner |
| Solution 2 |
