Reputation: 29
Whenever I use this line of code, spaces occur around the letter 'e', which I am trying to avoid. Even though this isn't a major problem in the code, it will just help to make it read better.
I have tried to reshuffle the layout of my code but there has been no success
print("The value of", '\033[1m', '\033[4m', "e", '\033[0m', "is", math.e)
The output is
The value of e is 2.718281828459045
But I would much prefer the result to be
The value of e is 2.718281828459045
(Has only 1 space around 'e')
(Please note that the letter 'e' is bold and underlined in the output so that is working correctly.)
Upvotes: 1
Views: 154
Reputation: 26886
To avoid print()
printing a space between its arguments, use the sep
keyword:
print('No', 'Space', '!', sep='')
which prints:
NoSpace!
However, for printing that string, it may be beneficial to use the f
qualifier for string interpolation (requires Python 3.6+) and a library for ANSI escaping, e.g. blessed
:
import math
import blessed
t = blessed.Terminal()
import math
print(f"The value of {t.bold}{t.underline}e{t.normal} is {math.e}")
For earlier versions of Python, you could use the .format(**locals())
construct, which is (almost) equivalent to the f
string interpolation:
import math
import blessed
t = blessed.Terminal()
import math
print("The value of {t.bold}{t.underline}e{t.normal} is {math.e}".format(**locals()))
(EDIT: added a bit more explanation).
Upvotes: 1
Reputation: 1828
No need to use string formatting and import blessed. Try this:
'\033[0m' = ResetAll
'\033[1m' = Bold
'\033[4m' = Underline
print ("The value of" + '\033[1m', '\033[4m' + "e" + '\033[0m',"is",math.e)
Output:
Upvotes: 1
Reputation: 2095
Use string formatting.
import math
print("The value of {} is {}".format("e", math.e)
If you want to include the bold and underlining:
import math
print("The value of {}{}{}{} is {}".format('\033[1m', '\033[4m', "e", '\033[0m', math.e))
Upvotes: 2