473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

None in string formatting

Was doing some string formatting, noticed the following:
x = None
"%s" % x

'None'

Is there a reason it maps to 'None'? I had expected ''.

Jul 18 '05 #1
5 1597
ro************@gmail.com wrote:
Was doing some string formatting, noticed the following:
x = None
"%s" % x


'None'

Is there a reason it maps to 'None'? I had expected ''.


Can you explain why you expected that? A few other examples that make
me not expect what you do:

py> '%s' % False
'False'
py> '%s' % []
'[]'
py> '%s' % {}
'{}'
py> '%s' % set()
'set([])'

All of the objects above evaluate to False in a boolean context like ''
does, but they display a string appropriate to their type. Since None
has it's own type (NoneType), I would expect similar behavior.

STeVe
Jul 18 '05 #2
ro************@gmail.com writes:
Was doing some string formatting, noticed the following:
x = None
"%s" % x

'None'

Is there a reason it maps to 'None'? I had expected ''.


How would know, then, if there was no value at all or if it was an empty
string?

--
Godoy. <go***@ieee.org>
Jul 18 '05 #3
Jorge Godoy wrote:
ro************@gmail.com writes:
Was doing some string formatting, noticed the following:
>x = None
>"%s" % x

'None'

Is there a reason it maps to 'None'? I had expected ''.


How would know, then, if there was no value at all or if it was an empty
string?


If you want the other effect, you can always do:

"%s" % (x or '')

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #4
Jorge Godoy wrote:
ro************@gmail.com writes:

Was doing some string formatting, noticed the following:

>x = None
>"%s" % x


'None'

Is there a reason it maps to 'None'? I had expected ''.

How would know, then, if there was no value at all or if it was an empty
string?

Well, for that matter, how can you tell the difference between

'%s' % False

and

'%s' % 'False'

since both inevitably produce the same output.

The bottom line is that you are trying to map the strings plus other
values on to the strings, which means it's a mathematical certainty
there will be ambiguities. It's just that you want *your* preferred
ambiguities rather than what Python gives you.

Suppose Python were to do what you want, how could you distinguish
between the outputs for "" and None? Of course, at the moment the
outputs for "None" and None are the same, but that just supports my
assertion about the inevitability of ambiguities.

regards
Steve

Jul 18 '05 #5
ro************@gmail.com wrote:
Was doing some string formatting, noticed the following:
x = None
"%s" % x

'None'

Is there a reason it maps to 'None'? I had expected ''.


Because %s just calls str on the arguments, and str(None) == 'None'.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
As far as I'm concerned, being any gender is a drag.
-- Patti Smith
Jul 18 '05 #6

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

Similar topics

5
by: Thomas Philips | last post by:
Consider the following simple dictionary e={1:'one', 2: 'two'} e >>>'one' However, If I attempt to print e using a formatted string print " %(1)s" %e, I get a KeyError: '1'
10
by: Oliver S. | last post by:
I've developed a string-class that holds the string in an array which is a member-variable of the class and that has maximum-size which is con- figurable through a template-parameter. If any...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
5
by: Andrew Connell | last post by:
Having fits transforming an XML string using an XSL file. In the 1.1 version of the framework, I see that the XmlResolver is heavily used in the XslTransform class. However, that looks like I am...
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
3
by: Stan Brown | last post by:
Posted by Pierre Blais in the current RISKS Digest: http://catless.ncl.ac.uk/Risks/24.24.html#subj9 -- Stan Brown, Oak Road Systems, Tompkins County, New York, USA http://OakRoadSystems.com/...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
14
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my...
1
by: schoedl | last post by:
Hello, we often compose strings via a ostringstream and then create a string from it. What is the rationale of not being able to use string in place of a ostringstream, so I could write ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.