Nested for loops and print statements

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Mon Sep 26 13:50:18 EDT 2016


Cai Gengyang writes:

> What is a tab and what is a space in python and what's the difference
> ?

Try print('x\tx') in Python to see a tab character between the two x's.
For me it looks the same as seven spaces, for you it will also look like
some amount of whitespace but it might be a different amount.

The two-character sequence \t inside a Python string literal means tab,
same as \x09, same as \u0009, same as \N{TAB}, same as \N{HT}. (These
sequences need to be inside quotes.)

With print('xx\tx'), the tab looks, for me, the same as six spaces. It's
funny that way: it's a single character, but it's shown differently
depending on it's position on the line.

You may get a tab character by pressing the tab key, but it's usual for
that key to be smart in some way or used for some other purpose.

> Which piece of code is indented with tabs and which one is indented
> with spaces ?

I've replaced the tabs with # below, and the greater-than signs with @.
This may not have been exactly your code any more, but try to imagine a
tab in place of each # anyway.

 @@@@ for row in range(10):
 @        for column in range(10):
 @ #  print("*",end="")
 @ #
 @ SyntaxError: inconsistent use of tabs and spaces in indentation
 @@@@ for row in range(10):
 @ #for column in range(10):
 @ #   print("*",end="")
 @

 It would be better for you to get a better editor that either prevented
you from getting into this mess in the first place, or at least showed
where the offending characters are.

It may be possible to configure your editor to be better. The details
depend on what exactly you are using. What are you using?



More information about the Python-list mailing list