44

I have a problem coding Python in terminal. I'm just learning basics so I have no need to create .py files.

In terminal I can run one line of code in the Python interpreter, but how do I write more than one line?

Obviously if I hit enter, it enters the command and doesn't go down a line.

I just want to test following in terminal:

my_age = 35
my_eyes = 'Blue'
print "my age is %d and my eye color is %s" % (my_age, my_eyes)

9 Answers9

38

Add a trailing backslash (\)

The trick is – similar to what you would do in bash, for example – to add a trailing backslash. For example, if I want to print a 1:

charon:~ werner$ python
>>> print 1
1
>>> print \
... 1
1
>>> 

If you write a \, Python will prompt you with ... (continuation lines) to enter code in the next line, so to say.

Side note: This is what automatically happens when you create a function or class definition, i.e. the times when you really need a new line, so there's never a really good use for that, or at least none that I know of. In other words, Python is smart enough to be aware that you need continuation lines when you are entering a new function definition or other similar constructs (e.g. if:). In these automatic cases, do note that you need to enter an empty line using \ to tell Python that you are done.

For everything else, you need to write one line after another. The way an interpreter works is that it, well, interprets every line that you feed it. Not more, not less. It will only "act" when it sees a newline, therefore telling the interpreter to execute what you gave it. The single backslash will prevent the interpreter from ever receiving a newline character (i.e. it won't know that you actually pressed Enter), but it will eventually receive one.

Python's interpreter has advanced capabilities when you use GNU readline, such as Emacs or vi-style keybindings to navigate within a line (e.g. Ctrl-A). Those however work only in the one current line. History is there as well, just try and press .

What if I want to run complicated lines over and over?

You probably want to use proper source files if you want to execute more than one line of code at a time.

Or, use Jupyter notebooks, which offer a great, interactive way to create Python code with a built-in interpreter. You can write code as you would in a source code editor, but you can choose which lines are interpreted together. You can then run only parts of the code selectively. The best way is to just try and see if that fits your workflow.

slhck
  • 235,242
12

How about using ;\? The semicolon signals the end of a command and the backslash signals that we are continuing on the next line. For example, type python at command line to get into Python interpreter, then

>>> x=0 ;\
... print(x) ;\
... x=4 ;\
... print(x)

should give an output of

0
4
Garrett
  • 286
  • 3
  • 9
5

I was just going through the answer which you have got.I kept on experimenting by putting different symbols.I finally got the correct syntax to write it.try the following

print("more string") ; print(3)

this will give you a result

more string

3

with no any error

i have just used ';' to make it write in another line

i hope my answer may help you

Toto
  • 19,304
2

Simply put, if you want to learn and want to run more than one line you write it into a .py file.

The trailing backslash method is good when you quickly want to run a series of commands, but it doesn't help when you are learning.

You will be able to develop code better, edit individual commands without worrying about spelling mistakes, and reuse code snippets you find useful if you write them into a small file.

Rory Alsop
  • 3,360
2

I just typed the following at my shell prompt, and it worked just fine:

$ python
Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> my_age = 35
>>> my_eyes = 'Blue'
>>> print "my age is %d and my eye color is %s" % (my_age, my_eyes)
my age is 35 and my eye color is Blue
>>> 

The way to type more than one line of code in the interactive Python interpreter is, well, to type more than one line of code in the interactive Python interpreter. I'd think that would be good enough for your purposes.

It's true that you'll get a new prompt after each line, which means that if two of your lines of code produce output, that output will be separated by prompts. I guess that's what you're concerned about, though the example in your question doesn't suggested that:

$ python
Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "first line"
first line
>>> print "second line"
second line
>>> 

If that's a problem, you can enclose your multiple statements in a (properly indented!) if statement:

$ python
Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> if 1:
...     print "first line"
...     print "second line"
... 
first line
second line
>>> 

(I'd suggest, even though it doesn't answer your question, that if you're writing code that's complex enough for this to matter, you should be writing scripts. Perhaps you've started doing so in the year and a half since you posted the question.)

1

I had the same beginner issue and the solution is simple. When you get the next line (...) hit ENTER again with nothing on the line and it will execute your code, whatever you have entered

Frank
  • 11
0

Just leave the shell and open a new editor file. I.e. go to file and open new file. Write as many lines of codes as you want.

0

go to file

open new file

THen New file will open

enter the commands

save it

THen press "run" in the main menu

enter image description here

-2

At the three dots make sure to hit "tab" key before you enter next command. You can continue writing as many commands that way. So when you hit enter after writing your second line of code, interpreter lets you enter third line of code... like in Fibonacci example below (from the tutorial):

a,b=0,1 while b<10: ... print(b) # If you enter print command immediately after the dots and hit enter key at the end, u will get an indentation error. Instead hit the tab after the three dots, then write your code and hit enter key, it will let u enter third line of code and so on. Look at below example (from tutorial):

  <p>a,b=0,1 # Followed by enter key
  while b&lt;10: # Followed by enter key
  ...    print(b) # Followed by enter key. note the tab after the three dots
  ...    a,b=b,a+b # Followed by enter key
  ...enter key # No more commands to enter</p>
</blockquote>

you will see the result of the above prog

Shashi
  • 1