New line

A forum for general discussion of the Python programming language.

New line

Postby glenmoe85 » Fri May 03, 2013 10:46 am

Hey Guys,

i need a big sentence to go onto a new line and looked and found \n. this doesn't work and wanted to know if im doing anything wrong.

Code: Select all
'testing': 'I am so great, \n Now get me a sandwich. '
glenmoe85
 
Posts: 7
Joined: Mon Apr 01, 2013 3:58 pm

Re: New line

Postby stranac » Fri May 03, 2013 10:54 am

Yes, \n is a newline character.
You should show us some code that you tried, so we can see what your problem is.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 896
Joined: Thu Feb 07, 2013 3:42 pm

Re: New line

Postby KssK » Mon May 06, 2013 12:03 am

I am utilizing 3.3.1 of python and the "\n" command works for me as well OP. if you are using 2.7, I remember I had to tweak the "\n" a little to make it operate properly.
Just a Knowledge seeker, seeking Knowledge
User avatar
KssK
 
Posts: 3
Joined: Sun May 05, 2013 11:52 pm
Location: Inner-Space

Re: New line

Postby Mekire » Mon May 06, 2013 3:20 am

KssK wrote: I remember I had to tweak the "\n" a little to make it operate properly.
How does one tweak "\n". Given the ops code and assuming he intended that to be part of a dictionary:
Code: Select all
d = {'testing': 'I am so great, \nNow get me a sandwich. '}
print(d["testing"])
Code: Select all
I am so great,
Now get me a sandwich.
Output is identical in python 2.7 and python 3.

-Mek
User avatar
Mekire
 
Posts: 818
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: New line

Postby Kebap » Mon May 06, 2013 9:02 am

Yeah why dictionary, just use the python shell and power of print for quick testing around:

Code: Select all
>>> 'I am so great, \n Now get me a sandwich. '
'I am so great, \n Now get me a sandwich. '
>>> print 'I am so great, \n Now get me a sandwich. '
I am so great,
 Now get me a sandwich.
>>>
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: New line

Postby KssK » Mon May 06, 2013 3:52 pm

Mekire wrote:
KssK wrote: I remember I had to tweak the "\n" a little to make it operate properly.
How does one tweak "\n". Given the ops code and assuming he intended that to be part of a dictionary:
Code: Select all
d = {'testing': 'I am so great, \nNow get me a sandwich. '}
print(d["testing"])
Code: Select all
I am so great,
Now get me a sandwich.
Output is identical in python 2.7 and python 3.

-Mek


Well I am brand new to coding/programming in general, and I think I was using 2.7 at first. when I used the "\n" function for some odd reason, the IDLE did not execute it in the way I was expecting or had read about, I also noticed that if I added a space after "\n" it would print such as this....
Code: Select all
print ("This is a test and line one.\n This is a test and line two.")

executes to...
Code: Select all
This is a test and line one
 This is a test and line two.


so I founded out I had to remove the space after the "\n" and also just typing "print" followed by the code didn't work out as I had expected it, I had to put up "()" around the code as well as " at the entrance and exit's of the brackets.

Again, I am new to this so it could have easily been an error on my behalf, but figured I would alert the OP to this as it was a work around.
Just a Knowledge seeker, seeking Knowledge
User avatar
KssK
 
Posts: 3
Joined: Sun May 05, 2013 11:52 pm
Location: Inner-Space

Re: New line

Postby Mekire » Mon May 06, 2013 4:02 pm

Yeah, the "\n" is literally a newline character so if you put a space after it, that will definitely show up on the next line. As for needing the parenthesis it sounds like you were reading a python 2.x tutorial but using python 3. Python 3 requires parens as print is now a function (it was previously a statement). I personally think it is best practice to include the parens in 2.x code as well just so you don't break compatibility for such a silly reason.

-Mek
User avatar
Mekire
 
Posts: 818
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: New line

Postby KssK » Mon May 06, 2013 4:54 pm

Mekire wrote:Yeah, the "\n" is literally a newline character so if you put a space after it, that will definitely show up on the next line. As for needing the parenthesis it sounds like you were reading a python 2.x tutorial but using python 3. Python 3 requires parens as print is now a function (it was previously a statement). I personally think it is best practice to include the parens in 2.x code as well just so you don't break compatibility for such a silly reason.

-Mek


Yea, I believe the book in which I was reading was for 2.x and I figured that maybe the OP was doing the same thing I was doing.
Just a Knowledge seeker, seeking Knowledge
User avatar
KssK
 
Posts: 3
Joined: Sun May 05, 2013 11:52 pm
Location: Inner-Space


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests