1,105,344 Community Members

how do i go onto a new line in python

Member Avatar
darkfury18
Newbie Poster
3 posts since Feb 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

for example if i write hello on one line and then wanted to write goodbye right underneath what do i have to do? as when i press the enter key it just prints hello

Member Avatar
scru
Posting Virtuoso
1,624 posts since Feb 2007
Reputation Points: 909 [?]
Q&As Helped to Solve: 165 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

The newline character in python is '\n'.

print "Hello\nGoodbye"

or for python 3:

print("Hello\nGoodbye")

The print statement in python (except python 3) automatically adds a newline at the end.

Member Avatar
Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 299 [?]
Q&As Helped to Solve: 119 [?]
Skill Endorsements: 0 [?]
 
1
 

The print statement in python (except python 3) automatically adds a newline at the end.

Should be noted that this can be prevent by doing the following

print "Hello World",

Yup a trailing ','
:)

Chris

Member Avatar
Paul Thompson
Veteran Poster
1,095 posts since May 2008
Reputation Points: 178 [?]
Q&As Helped to Solve: 189 [?]
Skill Endorsements: 3 [?]
 
1
 

The print statement in python (except python 3) automatically adds a newline at the end.

Wait, i think that in python 3 there is an automatic \n at the end of lines. Here are the default args for print()

print - sep = ' ', end = '\n', file = sys.stdout

So that seems to point towards print actually adding a new line to the end.

Also if you do not want this then just go

print ("Hello there", end = '')
Member Avatar
scru
Posting Virtuoso
1,624 posts since Feb 2007
Reputation Points: 909 [?]
Q&As Helped to Solve: 165 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

Oh right.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article