1

I have

>>>i=65536
>>>print('The value of i is', i)
The value of i is 65536

How do I get the output (notice the lack of space between is & 65536)

The value of i is65536

Without manipulating the strings prior to the print()

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • 3
    When you have a question like this, it's usually worth either typing `help(print)` into the interactive interpreter, or searching [the online docs](https://docs.python.org/3/library/functions.html#print). (Or, if you use an IDE that has built-in help, search that instead.) Most answers—including this one—have an easy answer that you'll spot immediately. – abarnert Aug 08 '14 at 00:54
  • 2
    I am going with the python.org tutorial. Have not seen it there, maybe it comes later. Will remember the help(print) though, they did not mention that – Itay Moav -Malimovka Aug 08 '14 at 00:57
  • @ItayMoav-Malimovka: I didn't realize it doesn't mention the `help` command early on. That really ought to be covered right when they introduce the interactive interpreter in chapter 2. You want to file a docs bug on that? – abarnert Aug 08 '14 at 01:19

4 Answers4

6

There are two choices.

First, you can use the sep keyword argument to the print function:

print('The value of i is', i, sep='')

Or, better, you can use string formatting instead of a multi-argument print call:

print('The value of i is{}'.format(i))

The second one is a lot more flexible, and more readable for any but the simplest cases. But either one works.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    Although not pertinent to this Python 3.x tagged question, FWIW, another advantage of the `format()` string method approach is that it would work in Python 2.6+. Similarly, `print('The value of i is' + format(i))` which uses the built-in `format()` function also works in both versions. – martineau Aug 08 '14 at 01:14
  • @martineau: Good point, which may be helpful to future searchers coming off the `[python]` tag rather than `[python-3.x]` (or just to someone who needs to write a single codebase that's 2.6+/3.2+, which is still a pretty common target). Except that with 2.6, you'd need `{0}` instead of `{}`; you need 2.7 or… I think 3.2?… to leave out the indices. – abarnert Aug 08 '14 at 01:16
2
print('The value of i is', i, sep='')
kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
0

print('The value of i is{my_var}'.format(my_var=i))

or

print('The value of i is' + str(i))

Nagra
  • 466
  • 3
  • 8
0

Another way you could do it is:

i=65536 print('The value of i is', end='') print(i)

When you do:

end=''

You are making it so it does not skip to the next like after finish that print statement.

user3920324
  • 3
  • 1
  • 3