Home > database >  ANSI escape sequence "\033[1m" printing bold for rest of program instead of specified lin
ANSI escape sequence "\033[1m" printing bold for rest of program instead of specified lin

Time:02-17

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?")

CodePudding user response:

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")

See here

CodePudding user response:

You need to end the escape sequence. Using \033[0m will reset all formatting.

print("\033[1m" "Title" "\033[0m")
  • Related