How to Print Variables Without Spaces Between Values

How to print variables without spaces between values

Don't use print ..., (with a trailing comma) if you don't want spaces. Use string concatenation or formatting.

Concatenation:

print 'Value is "' + str(value) + '"'

Formatting:

print 'Value is "{}"'.format(value)

The latter is far more flexible, see the str.format() method documentation and the Formatting String Syntax section.

You'll also come across the older % formatting style:

print 'Value is "%d"' % value
print 'Value is "%d", but math.pi is %.2f' % (value, math.pi)

but this isn't as flexible as the newer str.format() method.

In Python 3.6 and newer, you'd use a formatted string (f-string):

print(f"Value is {value}")

Print without space in python 3

You can use the sep parameter to get rid of the spaces:

>>> print("a","b","c")
a b c
>>> print("a","b","c",sep="")
abc

I don't know what you mean by "Java style"; in Python you can't add strings to (say) integers that way, although if a and b are strings it'll work. You have several other options, of course:

>>> print("a = ", a, ", b = ", b, sep="") 
a = 2, b = 3
>>> print("a = " + str(a) + ", b = " + str(b))
a = 2, b = 3
>>> print("a = {}, b = {}".format(a,b))
a = 2, b = 3
>>> print(f"a = {a}, b = {b}")
a = 2, b = 3

The last one requires Python 3.6 or later. For earlier versions, you can simulate the same effect (although I don't recommend this in general, it comes in handy sometimes and there's no point pretending otherwise):

>>> print("a = {a}, b = {b}".format(**locals()))
a = 2, b = 3
>>> print("b = {b}, a = {a}".format(**locals()))
b = 3, a = 2

How to print a variable and a string in Python without a space between

There are several ways to remove the space delimiter. Fortunately print() only inserts a space between fields by default. From the print() doc:

"sep: string inserted between values, default a space."

So my preferred way would be to use an empty string:

print(a, ": string of text", sep="")

How to print without spaces in python 3?

You can use the sep argument to print:

>>> income = 50000
>>> print("Your income tax is $", income, ".", sep='')
Your income tax is $50000.

Or use the str.format:

>>> print("Your income tax is ${}.".format(income))
Your income tax is $50000.

Removing Space between variable and string in Python

There are several ways of constructing strings in python. My favorite used to be the format function:

print "Hello {}!".format(name)

You can also concatenate strings using the + operator.

print "Hello " + name + "!"

More information about the format function (and strings in general) can be found here:
https://docs.python.org/2/library/string.html#string.Formatter.format

6 Years Later...

You can now use something called f-Strings if you're using python 3.6 or newer. Just prefix the string with the letter f then insert variable names inside some brackets.

print(f"Hello {name}")

How to print a string of variables without spaces in Python (minimal coding!)

Try using join:

print "\n"+'|'.join([id,var1,var2,var3,var4])

or if the variables aren't already strings:

print "\n"+'|'.join(map(str,[id,var1,var2,var3,var4]))

The benefit of this approach is that you don't have to build a long format string and it basically works unchanged for an arbitrary number of variables.

Removing space between an integer and fullstop

Simply change your code to :

sol=(num1+num2)*(num1+num2)
print("\nThe Solution is",str(sol) + '.')

How to print without a newline or space

In Python 3, you can use the sep= and end= parameters of the print function:

To not add a newline to the end of the string:

print('.', end='')

To not add a space between all the function arguments you want to print:

print('a', 'b', 'c', sep='')

You can pass any string to either parameter, and you can use both parameters at the same time.

If you are having trouble with buffering, you can flush the output by adding flush=True keyword argument:

print('.', end='', flush=True)

Python 2.6 and 2.7

From Python 2.6 you can either import the print function from Python 3 using the __future__ module:

from __future__ import print_function

which allows you to use the Python 3 solution above.

However, note that the flush keyword is not available in the version of the print function imported from __future__ in Python 2; it only works in Python 3, more specifically 3.3 and later. In earlier versions you'll still need to flush manually with a call to sys.stdout.flush(). You'll also have to rewrite all other print statements in the file where you do this import.

Or you can use sys.stdout.write()

import sys
sys.stdout.write('.')

You may also need to call

sys.stdout.flush()

to ensure stdout is flushed immediately.

How to prevent different sized spaces between strings?

You could try string formatting

"{:10s} {:s}".format(variable1, " - Age 40")

where 10 is the number of characters you want your variable1 to take up. If its length is less than that, it is padded with spaces. Similarly for the second string.

However, your - hyphen is part of the second string, so it can't be padded, unless you split your string manually.



Related Topics



Leave a reply



Submit