What is whitespacing?

Hi, I’m new to programming and I’m trying to learn a bit before I start a class in a few months. And I’m just going though the lessons again and I don’t understand what whitespacing means and what it does :sweat_smile: If I could get some clarification on it that would be great! Thanks :slight_smile:

Im still confused on what It is :sweat_smile: Can you put it in simpler terms? Thanks

newline, carriage return, tab, space

1 Like

It means:

if 'a' == 'a':
    #Whitespace is to the left, also known as indentation

Any character that only shows up as white space, as in showing just the background that the characters are printed on, is whitespace. Indentation usually consists of whitespace characters.

@thesquidcod in the most basic and simple terms when u press an enter key or a space bar or the tab key all give you white spaces…
for example when you press the spacebar the cursor moves one position forward leaving a blank space between two words. This blank space is known as a white space in programming language. The same applies to when you press tab and enter keys…
Hope this helps. :wink:

1 Like

Excellent, thank you :slight_smile: my question is, but why? Why do you sometimes need to put four spaces in front of some lines? What purpose does it serve?

The lessons are telling me to do things and I want to know WHY I am!! haha

Indentation (which is whitespace, but not all whitespace is indentation) is part of the syntax of certain statements. For example, a function has a def keyword, then the name, then a list of arguments in parenthesis followed by a comma. After that follows the body of the function which is indented, in other words, when something is no longer indented it’s no longer part of that function’s body.

def f():
    inside body

@tagsolver10022 see basically speaking the 4 white spaces are basically
done by using tab. And for your answer as to why we need those white spaces
they are a part of the python syntax. See as basically java, c, cpp
programming languages use curly braces as syntax between your code python
distinguishes the same by indentation instead of braces hence we need those
white spaces which act as indentation and are part of the syntax of Python.

Spaces aren’t “white spaces”, they are just spaces.
If you mean any four whitespace characters, which I’m sure you’re not, then you would be saying that linebreaks could be used as indentation.

TAB is another character, TAB is not space, but it is a whitespace character. Editors commonly replace tab characters with spaces, though some may prefer the tab characters which can also be used as indentation and different languages may have different recommendations about which to use.

@ionatan i understand spaces are not whitespaces and tab is another character but instead of confusing tagsolver i explained him in a simple manner as eventually over the course tagsolver would understand what are the meaning and use of whitespaces…

@ronit1710, @ionatan - Kindly check this link below:

This is the error code:
def spam():
eggs = 12
return eggs
print spam()

Here, in the instruction, it is given “Properly indent the code with four spaces before eggs on line 2 and another four before return on line 3.
You should indent your code with four spaces.”

I’ve just given 1 space before the line 2 & 3 like below:

def spam():
eggs = 12
return eggs
print spam()

and it just worked. So, what is the purpose of giving exactly 4 spaces in the code as specified in the instruction?

@hariramesh the code they have asked you to indent is just an example so it won’t give any error.
However python is very strict about syntax and so it is a part of the python syntax to indent the block and so if you don’t indent properly with four spaces in future exercises or even on proper compilers it will give you indentation errors.
That’s the reason you need to give four spaces. Hope this helped…

---- Hariramesh wrote ----

Thankyou @ronit1710. But, why is it four spaces? why can’t it be some other number of spaces? On what basis does the number of spaces depend?

@hariramesh most welcome and secondly it can’t be any other number the
reason being it is a part of the python syntax. If you have done
programming in Java aur c++ then you may know that curly braces are
compulsion as a part of the syntax. If you put an extra curly brace or less
curly braces then the Compiler gives and error.
The python compiler is Much similar to this just that it uses 4 spaces
instead of the curly braces.
So the number is fixed at 4 being a part of the syntax.

1 Like

Thanks for the immediate response @ronit1710. :slight_smile:

You can use any number of spaces or tabs (don’t mix), python doesn’t care how many you use, just that indentation levels are correct. PEP8 recommends 4 spaces.

@ronit1710 please be careful not to make too much up when you give advice, you can do quite a bit of damage that way. Aside from indentation not being fixed at 4 spaces, python is normally not compiled, the reference implementation is an interpreter. Java and C are perfectly happy to let you add extra curly braces, this is used to group statements which is needed for loops which only have a body of a single statement, and in C it also has the effect of creating a new scope.
Make it very clear when you’re guessing or leaving things out.

Thank you so much, that is super clear!! Thanks man ^.^

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.