Python output?

Joined
Mar 29, 2023
Messages
16
Reaction score
1
I am learning Python3 and while doing some revision i wrote out a simple 'for loop' and noticed that when i wanted the output on an
horizontal level it outputted the string and the python prompt on the same line.

Here is a sample of Python code i used:

Code:
         >>> programming = 'python.code'
         
         >>> for program in programming :
         ...    print(program)
         ...
       python.code>>>

I have tried using the print() function like print(program, end='\n')
But this results in the output being printed on a vertical line.

Hope someone can help a noob like myself.

Thanks all
 
Joined
Jul 4, 2023
Messages
575
Reaction score
77
Have you tried?
Python:
programming = 'python.code'
for program in programming:
  print(program, end='')
print('\n')
 
Joined
Jan 14, 2025
Messages
25
Reaction score
6
I am learning Python3 and while doing some revision i wrote out a simple 'for loop' and noticed that when i wanted the output on an
horizontal level it outputted the string and the python prompt on the same line.

Here is a sample of Python code i used:

Code:
         >>> programming = 'python.code'
        
         >>> for program in programming :
         ...    print(program)
         ...
       python.code>>>

I have tried using the print() function like print(program, end='\n')
But this results in the output being printed on a vertical line.

Hope someone can help a noob like myself.

Thanks all
The issues you face are from the "\n" part. This creates a new line.
Modify the print() function by using the end parameter. Setting end='' will prevent the newline character from being added after each print statement.

What VBService posted is a perfect example:
Code:
programming = 'python.code' 
for program in programming:    
print(program, end='')
Hope this helps :)
 
Joined
Mar 29, 2023
Messages
16
Reaction score
1
Hi Moorcam

I am running MX Linux 23.5 Libretto. Could this be the problem with the terminal producing the Python prompt at the end of the output?

I have tried writing the same code into an Idle shell and it displayed the correct output and without the prompt.

But when i opened up a terminal the prompt was displayed at the end of the output?

So i think i'll have to stick with the Idle shell from now on.

Big thanks for taking the time out to reply.
 
Joined
Jan 14, 2025
Messages
25
Reaction score
6
Hi Moorcam

I am running MX Linux 23.5 Libretto. Could this be the problem with the terminal producing the Python prompt at the end of the output?

I have tried writing the same code into an Idle shell and it displayed the correct output and without the prompt.

But when i opened up a terminal the prompt was displayed at the end of the output?

So i think i'll have to stick with the Idle shell from now on.

Big thanks for taking the time out to reply.
The >>> prompt indicates that Python is ready for more input. In your case, it seems like the terminal is not exiting the interactive mode after executing your loop. Try to run your Python code in a script file instead of the interactive shell.
 
Joined
Feb 14, 2025
Messages
1
Reaction score
0
Hey there! 😊

You're on the right track by using the end parameter in print(), but to keep the output on the same line, you should set end='' (an empty string) instead of '\n', which is the default newline character.

Try this:

programming = 'python.code'

for program in programming:
print(program, end='') # Prints characters on the same line

This will output:
python.code

If you want spaces between characters, you can modify it like this:

for program in programming:
print(program, end=' ')

This will output:


p y t h o n . c o d e

Let me know if you have any questions! Keep going—you're doing great!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,249
Messages
2,571,244
Members
47,876
Latest member
Kiptechie

Latest Threads

Top