1

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

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Mr Haaris
  • 29
  • 1
  • 1
    Possible duplicate of [How to print without spaces in python 3?](https://stackoverflow.com/questions/29528767/how-to-print-without-spaces-in-python-3) – mkrieger1 Jul 16 '19 at 13:48
  • does this statement `print("The value of","e", "is",math.e)` is not giving you the expected output – Ryuzaki L Jul 16 '19 at 14:01
  • @Deadpool that does not work because of the escape sequences they are using in their print statements. – blackbrandt Jul 16 '19 at 14:04
  • i'm just wondering either he only need `e` and `math.e` or he need all of the parameters? @blackbrandt – Ryuzaki L Jul 16 '19 at 14:06
  • I have added an answer. Hope it will help you. – GOVIND DIXIT Jul 16 '19 at 14:07
  • @Deadpool the terms that look like `\033[1m` are used to make the output bold and underlined. In a python terminal, paste his output vs your output and see the difference. – blackbrandt Jul 16 '19 at 14:08

3 Answers3

2

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))
blackbrandt
  • 2,010
  • 1
  • 15
  • 32
1

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

norok2
  • 25,683
  • 4
  • 73
  • 99
1

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:

enter image description here

GOVIND DIXIT
  • 1,748
  • 10
  • 27
  • Of course it is not **needed**, but it is a much better framework to use! By the same logic, there is no need to use Python, you might as well write your own assembly... – norok2 Jul 16 '19 at 14:50
  • There can be many solutions to this by using x formatting or y import but making the approach of the person work with min changes to his code is what I think he looks for. I guess. – GOVIND DIXIT Jul 16 '19 at 15:58
  • I thought the goal would be teach/learn best coding practices, but I guess not everybody is after that. Of course, if the metric is the character difference from OP code, then this approach fares better. – norok2 Jul 16 '19 at 17:25
  • You are absolutely right master I totally agree with you but don't know why people prefer this way, Maybe because of hurry in solving the issue at the time. – GOVIND DIXIT Jul 16 '19 at 17:32