Sublime Forum

Python question for beginner programmer

#1

Hello, new to both Python and Sublime Text. When I try to run the len() function in Sublime Text doesn’t return anything. Steps I’ve taken: validated the function works in the Python interpreter, ensure I have saved the file with a “.py” extension, and configured Sublime Text to run Python by hitting Cmd+B (build). I’ve done some Google searches and checked stack overflow but can’t seem to find any answers.

Thanks in advance for your help,
Mike

0 Likes

#2

It’s hard to say for sure what the problem might be because you haven’t included any sample code.

However from the sounds of your problem my first guess would be that you’re running afoul of the difference between running your code in an interactive REPL and running it as a “regular” script.

Lets assume the code you’re using looks like this:

len(["a", "b", "c"])

In a REPL (i.e. run python from the terminal and then manually enter the code, or select View > Show Console from the menu and enter the line of code there) you see this:

>>> len(["a", "b", "c"])
3

If you were to put this in a file like sample.py and run it, you don’t see anything at all.

The reason for that is that the len function is responsible for determining the length of things; if you want to see the value it returns, you need to print() it:

print(len(["a", "b", "c"]))
1 Like

#3

Thank you! Your assumptions were spot on. Great explanation, perfectly answers my question. Also a good call for me to include sample code in the future.

If you don’t mind me asking, how did you highlight / shade the sample code you provided?

Thanks again,
Mike

1 Like

#4

Anything that you post that’s indented with four spaces is automatically set as pre-formatted text in a block:

print(len(["a", "b", "c"]))

A quicker way is to fence your code by surrounding it with lines of three back ticks. In the post editor, it would look like this:

```
print(len(["a", "b", "c"]))
```

Doing it that way doesn’t require you to indent anything first, and as an added benefit you get syntax highlighting applied if the forum can determine the language that you’re using:

print(len(["a", "b", "c"]))
1 Like

#5

Great, thanks again for your help. Much appreciated.

1 Like