You should be able to use the format method:
"Location: {0:20} Revision {1}".format(Location, Revision)
You will have to figure out the format length for each line depending on the length of the label. The User line will need a wider format width than the Location or District lines.
Hi everyone!
I'm totally new to programming and just started learning python. As I don't want to get into the habit of wrongly formatted code I'll rather ask this question as google overwhelmed me with tons of stufff that was already way advanced. Is there any downside using spaces in your code? For example writing:
x = 1
if x < 2 :
print("small")
elif x < 10 :
print ("medium")
else :
print ("large")
print ("All done!")Instead of writing:
x=1
if x<2:
print("small")
elif x<10:
print("medium")
else:
print("large")
print("All done!")Thanks!
PEP indentation using 4 spaces vs. Google's 2 spaces
How to make output have spaces between words?
Python: using 4 spaces for indentation. Why? - Stack Overflow
Spacing in python - Stack Overflow
You should be able to use the format method:
"Location: {0:20} Revision {1}".format(Location, Revision)
You will have to figure out the format length for each line depending on the length of the label. The User line will need a wider format width than the Location or District lines.
Try %*s and %-*s and prefix each string with the column width:
>>> print "Location: %-*s Revision: %s" % (20,"10-10-10-10","1")
Location: 10-10-10-10 Revision: 1
>>> print "District: %-*s Date: %s" % (20,"Tower","May 16, 2012")
District: Tower Date: May 16, 2012
I'm currently reading the Python introduction by google, and I'm intrigued as to why the standard spacing is recommended to be 4 but google uses 2:
A common question beginners ask is, "How many spaces should I indent?" According to the official Python style guidelines (PEP 8), you should indent with 4 spaces. (Fun fact: Google's internal style guideline dictates indenting by 2 spaces!)
Why would their internal style guideline recommend a standard of two spaces instead of the normal 4? Is it related to how using * and + are "overload" operators as the introduction says? Would a py script that uses 2 spaces instead of 4 run quicker, or am I on the wrong hunch?
In the code comment, we hinted that * works faster than +, the reason being that * calculates the size of the resulting object once whereas with +, that calculation is made each time + is called. Both + and * are called "overloaded" operators because they mean different things for numbers vs. for strings (and other data types).
https://developers.google.com/edu/python/introduction
I'm trying to make a simple program that greets the user after they enter their name, but when I enter a name into the input, there is no space between 'Hello' and name. For example, if I input Michael as the name, the output shows up as HelloMichael. How can I get it to have a space? What am I missing here?
print('What is your name')
name = input()
print('Hello' + (name))
Everyone else uses 4 spaces. That is the only reason to use 4 spaces that I've come across and accepted. In my heart, I still want to use tabs (1 indent character per indent, makes sense, no? Separate indent from other whitespace. I don't care that tabs can be displayed as different widths, that makes no syntactic difference. The worst that can happen is that some of the comments don't line up. The horror!) but I've accepted that since the python community as a whole uses 4 spaces, I use 4 spaces. This way, I can assemble code from snippets others have written, and it all works.
I like the fact that four space characters nicely indents the inner code of a function, because def + one space makes four characters.
def·foo():
····pass
Problems from the point of view of Python syntax, no.
But I recommend against doing this, purely from a code maintainability point of view. What happens for example if you now need to add another variable to the list?
cheques = 1
x = 2
numberOfBACS = 87
ohdearwhatalongvariablenamethisishardlyworthtypingbutnevermind = 10
Now you have to edit all the lines, not just the one you've added.
I recommend you stick to pep8 unless you really need an extension.
Which makes for
cheques = 1
x = 2
bacs_count = 87
As Daniel pointed out, fixed space layout is problematic with any refactoring.