473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Typing \n in strings

Python 2.3.3 on Win2K. In the Python tutorial it says that typing \n in
string literals is the new-line character. I open the interpreter and type a
string with a \n in it but instead of outputting a string with a new line,
it outputs the \n as literal characters of the string. Is the tutorial
wrong, is the interpreter broken, or what is happening ?
Jul 18 '05 #1
4 1311
"Edward Diener" <el******@earthlink.net> wrote in message
news:RG*******************@newsread2.news.atl.eart hlink.net...
Python 2.3.3 on Win2K. In the Python tutorial it says that typing \n in
string literals is the new-line character. I open the interpreter and type a string with a \n in it but instead of outputting a string with a new line,
it outputs the \n as literal characters of the string. Is the tutorial
wrong, is the interpreter broken, or what is happening ?


What exactly are you entering and what is output?

Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
"now\nis\nthe\ntime" 'now\nis\nthe\ntime' print "now\nis\nthe\ntime" now
is
the
time s = 'now\nis\nthe\ntime'
s 'now\nis\nthe\ntime' print s

now
is
the
time
Jul 18 '05 #2
Edward Diener wrote:
Python 2.3.3 on Win2K. In the Python tutorial it says that typing \n in
string literals is the new-line character. I open the interpreter and type a
string with a \n in it but instead of outputting a string with a new line,
it outputs the \n as literal characters of the string. Is the tutorial
wrong, is the interpreter broken, or what is happening ?


Notice that there are also quotation marks around that string? What
you are seeing is _not_ the string itself, but a printable
representation of the string. Since it contains a newline, which is
not "printable" (by this definition, anyway) it converts it to \n
again. Trust that the string really does contain the ASCII LF
character (byte value 10) however. To prove it, and see the real
string, just print it:
'test\nme' 'test\nme' print 'test\nme' test
me

Does that help?

Remember always that the interactive interpreter shows you the
expression using the repr() call, which with strings will give
you a representation that makes it clearer what non-printable
characters are contained therein. Here's something that might
clear up any remaining questions:
print repr('test\nme')

'test\nme'

(Or, it might just raise more questions in your mind...
experimenting a little more should answer most of them. :-)

-Peter
Jul 18 '05 #3
Paul Watson wrote:
"Edward Diener" <el******@earthlink.net> wrote in message
news:RG*******************@newsread2.news.atl.eart hlink.net...
Python 2.3.3 on Win2K. In the Python tutorial it says that typing \n
in string literals is the new-line character. I open the interpreter
and type a string with a \n in it but instead of outputting a string
with a new line, it outputs the \n as literal characters of the
string. Is the tutorial wrong, is the interpreter broken, or what is
happening ?


What exactly are you entering and what is output?

Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
"now\nis\nthe\ntime" 'now\nis\nthe\ntime' print "now\nis\nthe\ntime"

now
is
the
time


Right you are and my faux pas. I didn't notice the print statement in the
tutorial.
Jul 18 '05 #4
>>> a = "line1\nline2"
a # This prints repr(a) 'line1\nline2' print a # This prints a

line1
line2

When you enter an expression in the interactive interpreter, and its
result is not None, Python shows you the repr() of the value. When you
use 'print expression', you are shown the str() of the value.
Skip ahead to 7.1, "Fancier Output Formatting", for some more details.

Jeff

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: dterrors | last post by:
Will php 6 do strong typing and/or namespaces? I was shocked to find out today that there are some people who actually argue that weak typing is somehow better. I didn't even know there was a...
12
by: Michael Muller | last post by:
Is there currently any plan to introduce static typing in any future version of Python? (I'm not entirely sure that "static typing" is the right term: what I'm talking about is the declaration of...
12
by: Jason Tesser | last post by:
I work for at a college where I am one of 2 full-time developers and we are looking to program a new software package fro the campus. This is a huge project as it will include everything from...
15
by: Premshree Pillai | last post by:
How do I force static typing in Python? -Premshree Pillai ===== -Premshree http://www.qiksearch.com/] ________________________________________________________________________ Yahoo! India...
94
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++....
49
by: bearophileHUGS | last post by:
Adding Optional Static Typing to Python looks like a quite complex thing, but useful too: http://www.artima.com/weblogs/viewpost.jsp?thread=85551 I have just a couple of notes: Boo...
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
11
by: Joseph S. | last post by:
Hi all, how do I avoid typing the keyword "$this->" every time I need to reference a member of a class inside the class? (coming from a world of cozy auto-complete enabled Java / .Net IDEs I...
2
by: tavspamnofwd | last post by:
Referred here from the tutor list. ---------- Forwarded message ---------- From: Evert Rol Hi Tom, Ok, I wasn't on the list last year, but I was a few days ago, so persistence pays off;...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.