Demonic
Demonic

Reputation: 55

Spaces in Python arguments

Only just started learning Python a couple days ago.

When I type arguments, for example print "Hello, World", is there a reason why I should (or shouldn't) put a space between print and the string? or if I want the variable name = raw_input ("name?"), should I have a spave between name, =, raw_input etc? It seems to work fine without, but I don't want to start getting bad habits that will bite me in the future.

Upvotes: 1

Views: 279

Answers (3)

HelloUni
HelloUni

Reputation: 448

For example:

print"What's ur name?"
name=raw_input("Name: ")
print"Ok your name is %s."%name

The code above will work fine. Without errors. But read the code. It doesn't look nice and the level of readability is less. And if you write huge amounts of code like that, it'd make your head spin the next time you read it.

And this same code written as:

print "What's ur name?"
name = raw_input("Name: ")
print "Ok your name is %s" % name

This makes the code more readable and looks better.
So the answer to your question is that its to make your code organized and readable to you and others.

Upvotes: 0

root
root

Reputation: 80346

There's a style guide for python code - PEP 8. Whitespace is used to enhance readability. You should specifically take a look at Whitespace in Expressions and Statements, which basically says "Yes, add one space, but only one."

Upvotes: 1

Kenzo
Kenzo

Reputation: 3633

Python does have its own style guide (PEP 8), and it's definitely worth your time to read it. As far as what you're saying:

    More than one space around an assignment (or other) operator to align it with another.

    x = 1
    y = 2
    long_variable = 3

More here: http://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements

Upvotes: 5

Related Questions