Home >Backend Development >Python Tutorial >How to Print Values Without Spaces in Python?
Printing Values without Spaces in Python
In Python, when printing multiple values without spaces, you can encounter extra spaces in the output. Let's explore two issues and provide solutions:
Issue 1: Removing Spaces Between Values
To concatenate values without spaces, use the sep parameter in the print function. By default, sep is a space, but you can change it to an empty string to remove spaces.
<code class="python">>>> print("a", "b", "c") a b c >>> print("a", "b", "c", sep="") abc</code>
Issue 2: Using Java-Style String Concatenation
Java-style string concatenation does not work in Python. Instead, you can use the following approaches:
<code class="python">>>> a = 42 >>> b = 84 >>> print("a = " + str(a) + ", b = " + str(b)) a = 42, b = 84</code>
<code class="python">>>> print("a = {}, b = {}".format(a, b)) a = 42, b = 84</code>
<code class="python">>>> print(f"a = {a}, b = {b}") a = 42, b = 84</code>
<code class="python">>>> print("a = {a}, b = {b}".format(**locals())) a = 42, b = 84</code>
By following these techniques, you can effectively print values without additional spaces or use Java-style string concatenation in Python.
The above is the detailed content of How to Print Values Without Spaces in Python?. For more information, please follow other related articles on the PHP Chinese website!