in python 3.6 you could use fstrings which are well readable and slightly faster than the other format-methods :
print(f'Hello {fn} {ln}!')
Answer from Marvin Taschenberger on Stack Overflowin python 3.6 you could use fstrings which are well readable and slightly faster than the other format-methods :
print(f'Hello {fn} {ln}!')
It's a stupid question, but I have to know why it's adding this random space after a comma.
The default setting of print is such that comma adds whitespace after it.
One way of removing the space before ! here is doing :
print('Hello,',fn, ln, end='')
print('!')
Out : Hello, First Last!
Here the end= specifies what should be printed upon end of print() statement instead of the default newline.
Another far more easier method is just to concatenate the string. ie,
print('Hello,',fn, ln + '!')
python - How to print variables without spaces between values - Stack Overflow
string - Printing in Python without a space - Stack Overflow
How do you print two variables without a space between them?
python 2.7.5+ print list without spaces after the commas - Stack Overflow
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}")
Just an easy answer for the future which I found easy to use as a starter:
Similar to using end='' to avoid a new line, you can use sep='' to avoid the white spaces...for this question here, it would look like this:
print('Value is "', value, '"', sep = '')
May it help someone in the future.
Use print() function with sep=', ' like this::
>>> print(one, two, three, sep=', ')
1, 2, 3
To do the same thing with an iterable we can use splat operator * to unpack it:
>>> print(*range(1, 5), sep=", ")
1, 2, 3, 4
>>> print(*'abcde', sep=", ")
a, b, c, d, e
help on print:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
You can use the Python string format:
print('{0}, {1}, {2}'.format(one, two, three))
When I do something like this:
a = 1
b = 2
print(a, b)
It outputs: 1 2. I want it to output: 12. Forgive me if this is a dumb question lol
The data in hand is a list of numbers. So, first we convert them to strings and then we join the join the strings with str.join function and then print them in the format [{}] using str.format, here {} represents the actual joined string.
data = [1,2, 3, 4]
print(data) # [1, 2, 3, 4]
print("[{}]".format(",".join(map(repr, data)))) # [1,2,3,4]
data = ['aaa','bbb', 'ccc', 'ddd']
print(data) # ['aaa', 'bbb', 'ccc', 'ddd']
print("[{}]".format(",".join(map(repr, data)))) # ['aaa','bbb','ccc','ddd']
If you are using strings
data = ['aaa','bbb', 'ccc', 'ddd'] print("[{}]".format(",".join(map(repr, data))))
Or even simpler, get the string representation of the list with repr function and then replace all the space characters with empty strings.
print(repr(data).replace(" ", "")) # [1,2,3,4]
Note: The replace method will not work if you are dealing with strings and if the strings have space characters in them.
You can use repr, then remove all spaces:
>>> print repr([1,2]).replace(' ', '')
[1,2]
Make sure you have no spaces in every element.
print(3+5,2**3,17-9,32/4)
gives me the result:
8 8 8 8.0
I tried print(3+5,"\n",2**3,17-9,32/4)
but that give me the result
8
8 8 8.0
why is there a space on the second line? and how do i remove the space?
Hi!
I'm coding in Python for the first time, so forgive me if my question is pre-school basic.
Could someone explain what the difference is between using commas and pluses inside print()? I ask this because commas result in automatic spaces, whereas you have to add spaces inside each string when using pluses. See below
print(The product: " + product + " costs " + price + " dollars")
print("The product:", product, "costs", price, "dollars")To a newbie like me, it seems like using plus signs means a lot more work considering you have to manually input spaces. But I assume there is a syntactic (and perhaps also semantic) difference. Could someone enlighten me as to how these two separate lines of code differ from each other?
Thank you!
EDIT: I know that using plus means I have to change product -> str(product) and price -> str(product), but that just further proves my point... Doesn't it mean a lot more work?
In your second example, you're passing five arguments to print.print will then automatically convert all of its arguments to strings and print them separated with spaces. In your first example, you're building up one big string, and then passing it as a single argument to print. You can also use string formatting to a similar effect: print("The product: {} costs {} dollars".format(product, price)), or even print(f"The product: {product} costs {price} dollars") if you're using Python 3.6.
Beginner questions like this are usually more welcome at r/learnpython ;)