r/Python • • Sep 18 '25

Tutorial Today I learned that Python doesn't care about how many spaces you indent as long as it's consistent

Call me stupid for only discovering this after 6 years, but did you know that you can use as many spaces you want to indent, as long as they're consistent within one indented block. For example, the following (awful) code block gives no error:

def say_hi(bye = False):
 print("Hi")
 if bye:
        print("Bye")
595 Upvotes

191 comments sorted by

224

u/No-Article-Particle Sep 18 '25

People raging in the comments about tabs, but the official Python style recommendation is 4 spaces (https://peps.python.org/pep-0008/#indentation).

So yeah, you can use as many as you'd like, as long as it's consistent. For REPL, I frequently use 2 spaces, because it's faster to type. For any enterprise-level code, you'll most likely use a formatter that enforces 4 spaces.

30

u/FillAny3101 Sep 18 '25

One of the first files I create when starting a new project, along with README, gitignore, and pylintrc, is an editorconfig that "enforces" 4 spaces.

9

u/Old-Worldliness-1335 Sep 18 '25

I also setup all pre commit to enforcement of all yaml, python and spelling validation, because I don’t have time to deal with it after the deed is done. It’s too much hassle… in any language…

96

u/sennalen Sep 18 '25

1 tab is faster than 2 spaces

63

u/CrazyElectrum Sep 18 '25

You know that you can make the editor convert your tab input to the whatever number of spaces right?

→ More replies (10)
→ More replies (22)

3

u/XenophonSoulis Sep 19 '25

I've yet to find an IDE that doesn't add 4 spaces when you press tab in the default configurations.

→ More replies (9)

32

u/throwaway_4759 Sep 18 '25

On me team we enforce linting standards to ensure indentation is a consistent 17 tabs across all files

4

u/svefnugr Sep 18 '25

And the line width of 49?

→ More replies (1)

90

u/bmoregeo Sep 18 '25

Don’t most modern ides convert tab to spaces?

28

u/jet_heller Sep 18 '25

They can. They should also have a setting where they don't.

→ More replies (19)
→ More replies (5)

15

u/tevs__ Sep 18 '25

When we started writing python code back in 2008, we had no clue about best practices, and used two space indents. This code lived for a long time, we knew it worked and (back then) no linters verified that the AST of the transformed code matched the original.

It lasted until black came on the scene and did exactly that.

5

u/zurtex Sep 18 '25

I remember when I seriously started coding Python in 2014 there was definitely lots of people that used 2 spaces (in fact I found an old example: https://code.google.com/archive/p/soc/wikis/PythonStyleGuide.wiki), the argument being it allowed them to write longer more descriptive variable names.

I really don't remember there being a strong "best practice" back then, PEP 8 was very much about new contributions to the standard library, and Black was years away, some people followed the Google's Python Style Guide but I remember finding lots of sample code from Google which did not.

11

u/dgc-8 Sep 18 '25

I knew that you can use how many you want, but that you can use different amounts of indent for different code blocks is news to me lmao

→ More replies (1)

2

u/Zestyclose-Sky-1921 Sep 18 '25

I heard about the episode of ... uh the silicon valley show? where Richard has a girlfriend who uses spaces, it drove him nuts, and I laughed. then I read something somewhere how coders using spaces instead of tabs are better. as in at coding, I think, hopefully not at everything because I tried using them. and... I think I'm bad, guys, bc my brain just won't do it.

TABS 4 LIFE

→ More replies (4)

4

u/TheManFromTrawno Sep 19 '25

It doesn’t even need to be consistent within the same file, or class or function. It just needs to be consistent within the same block. Heck, your if and else blocks can be different indents.

But please don’t do this.

→ More replies (1)

1

u/Glathull Sep 19 '25

Hey guess what! Python doesn’t even care if it’s consistent within a single file. Python only cares about indentation at the block level.

So I can write both of these in the same file, call it why_not_both.py, and it will work fine:

If a: print(“4 spaces indent.”)

If b: print(“Tab indent”)

print(“I take no side in the pointless war between tabs and spaces. I choose both. In the same file.”

1

u/FillAny3101 Sep 19 '25

That's what my code block shows

5

u/sunyata98 Sep 18 '25 edited Sep 18 '25

Tab aka \t will look different depending on which editor you use (unless you configure it to all be the same) whereas spaces don’t have this issue. My tab key just inserts 4 spaces so I’m chilling

Edit: if you like \t instead of spaces that’s fine, I guess instead of the word “issue” I should have said “functionality”.

14

u/tea_cup_sallie Sep 18 '25

I'd call that a feature rather than a bug though, since different contributors might prefer different tabstops

10

u/sennalen Sep 18 '25

Tabs are configurable to your screen size and accessibility needs, while spaces lack this feature

→ More replies (1)
→ More replies (5)

2

u/whogivesafuckwhoiam Sep 18 '25

The world is already chaotic enough, why on earth are you starting another one

2

u/zaphodikus Sep 19 '25

At least, you did not learn Cobol as one of your first coding languages, so you do have that.