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. //docs.python.org/library/string.html#module-string
In this post, we will see how to add space in Python. Specifically, we will cover the following topics:
- Add a space between variables in print[]
- Add spaces to the left and right sides of a string
- Add spaces at the beginning of a string
- Add spaces at the end of a string
- Add spaces between characters in a string
- Add a space between string items
So, without further ado, let’s get started.
Add a space between variables in print[]
Often, when we print variables, we want to separate them by a space to make them more readable. If variables in print[] are separated by a comma, a space gets automatically added between them. Let’s take an example.
a = 4 b = 3 print[a, b] fname = "ashton" lname = "agar" print["His name is", fname, lname]
Output
4 3 His name is ashton agar
Next, we will see how to add spaces at different places in a string.
Add spaces to the left and right sides of a string
To add space to the left and right sides, we can use the center[] method. It takes the length of the returned string. Moreover, it is invoked on a string and outputs a new one. Consider the following example.
name = "ashton" format = name.center[len[name]+8] print[format]
Output
ashton
In the above example, the new string’s length is 14, i.e., 8 for spaces and 6 for name length. 4 spaces get added to the left and 4 to the right.
Add spaces at the beginning of a string
To add spaces at the beginning only, we can use the rjust[] method. It right aligns the given string. It takes the length of the formatted string like the center[] method does. Let’s see.
name = "ashton" format = name.rjust[len[name]+4] #add four spaces at the beginning print[format]
Output
ashton
Add spaces at the end of a string
The ljust[] method, on the other hand, can be used to add spaces to the end. Let’s see.
name = "ashton" format = name.ljust[len[name]+4] #add four spaces to the end print[format]
Output
ashton
Add spaces between characters in a string
Let’s see how to separate each character in a string by a space. For that, we can use the join[] method.
The join[] method takes an iterable [list, string] and returns a string in which all items of an iterable are joined by a separator.
Here, we will pass our input string to join[] and use space as a separator. Let’s see.
string = "cricket" formatted = " ".join[string] #separate each character by a space print[formatted]
Output
c r i c k e t
As you can see, we add space between each character of the string.
Add a space between string items
Using the join[] method, we can also add space between two or more string items. Let’s see.
str1 = "cricket" str2 = "football" str3 = "hockey" string = " ".join[[str1, str2, str3]] print[string]
Output
cricket football hockey
In the above example, we pass a list containing string items to the join[] method. It returns a new string having the list items joined by a space.
If you only want to display [print] strings in such a way, you can use the print[] method and separate them using a comma.
Hey guys! It’s me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path as well! Don’t let anyone tell you that this can’t be done. Sky’s the limit, really…as long as you BELIEVE in it! And it all starts right here..at Maschituts!