Python print space between variables

A quick warning, this a pretty wordy answer.

print is tricky sometimes, I had some problems with it when I first started. What you want is a few spaces in between two variables after you print them right? There's many ways to do this, as shown in the above answers.

This is your code:

count = 1
conv = count * 2.54
print count, conv

It's output is this:

1 2.54

If you want spaces in between, you can do it the naive way by sticking a string of spaces in between them. The variables count and conv need to be converted to string types to concatenate(join) them together. This is done with str().

print (str(count) + "           " + str(conv))
### Provides an output of:
1           2.54

To do this is the newer, more pythonic way, we use the % sign in conjunction with a letter to denote the kind of value we're using. Here I use underscores instead of spaces to show how many there are. The modulo before the last values just tells python to insert the following values in, in the order we provided.

print ('%i____%s' % (count, conv))
### provides an output of:
1____2.54

I used %i for count because it is a whole number, and %s for conv, because using %i in that instance would provide us with "2" instead of "2.54" Technically, I could've used both %s, but it's all good.

I hope this helps!

-Joseph

P.S. if you want to get complicated with your formatting, you should look at prettyprint for large amounts of text such as dictionaries and tuple lists(imported as pprint) as well as which does automatic tabs, spacing and other cool junk.

Here's some more information about strings in the python docs. http://docs.python.org/library/string.html#module-string

Add space between variables in Python #

Use a formatted string literal to add a space between variables in Python, e.g. result = f'{var_1} {var_2}'. Formatted string literals allow us to include expressions inside of a string by prefixing the string with f.

Copied!

var_1 = 'hello' var_2 = 123 result = f'{var_1} {var_2}' print(result) # 👉️ hello 123

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Copied!

my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: True

Make sure to wrap expressions in curly braces - {expression}.

You can use this approach to add space between as many variables as necessary.

Alternatively, you can use the str.join() method to add space between variables.

Copied!

var_1 = 'hello' var_2 = 123 result_2 = ' '.join(map(str, [var_1, var_2])) print(result_2) # 👉️ hello 123

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your list of variable contains numbers or other types, convert all of the values to string before calling join().

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

We used the map() function to convert the integer stored in var_2 to a string but this isn't necessary if you are only joining strings.

Copied!

var_1 = 'hello' var_2 = 'world' result_2 = ' '.join([var_1, var_2]) print(result_2) # 👉️ hello world

Alternatively, you can use the str.format() method.

Copied!

var_1 = 'hello' var_2 = 123 result = '{} {}'.format(var_1, var_2) print(result) # 👉️ 'hello 123'

The str.format method performs string formatting operations.

Copied!

first = 'James' last = 'Doe' result = "His name is {} {}".format(first, last) print(result) # 👉️ "His name is James Doe"

The string the method is called on can contain replacement fields specified using curly braces {}.

You can also use the addition (+) operator to add a space between two variables, but make sure they are of compatible types.

Copied!

var_1 = 'hello' var_2 = 123 result = var_1 + ' ' + str(var_2) print(result) # 👉️ 'hello 123'

Notice that we used the str() class to convert the integer to a string so we can concatenate the variables with a space in between.

If you need to add multiple spaces between variables, use the multiplication operator to make your code more readable.

Copied!

var_1 = 'hello' var_2 = 123 result = var_1 + ' ' * 3 + str(var_2) print(repr(result)) # 👉️ 'hello 123'

The multiplication operator can be used to repeat a string a specified number of times.

Copied!

print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'

How do you print two variables with spaces in Python?

To print multiple variables in Python, use the print() function. The print(*objects) is a built-in Python function that takes the *objects as multiple arguments to print each argument separated by a space. There are many ways to print multiple variables. A simple way is to use the print() function.

How do you print a space in Python?

The Python String isspace() method is used to determine whether an argument has all whitespace characters such as:.
' ' – Space..
'\t' – Horizontal tab..
'\v' – Vertical tab..
'\n' – Newline..
'\r' – Carriage return..
'\f' – Feed..

How do you print two variables without space in Python?

Print Values Without Spaces in Between in Python.
Use the String Formatting With the Modulo % Sign in Python..
Use the String Formatting With the str.format() Function in Python..
Use String Concatenation in Python..
Use the f-string for String Formatting in Python..
Use the sep Parameter of the print Statement in Python..

How do you put a space in a string in Python?

The ljust method pads the end of the string to the specified width with the provided fill character. An alternative solution is to use the multiplication operator to add a specific number of spaces to the end of the string. Copied! What is this? ... .