473,811 Members | 3,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1620
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***********@A cm.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
3025
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
4848
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 operation would grow the string beyond its maximum size, an exeception would be thrown. This kind of string obviously has superior performance over a std::string because there's never any additional memory-allocation. But before I'm going to re-invent...
20
11333
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 expressions and I sure there is a lot ways, but I need realy efficient one
5
6176
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 only supposed to use that only when you have an xml ~file~... not an XML string (my XML is coming from a database field). Is there a cut & dry example out there on how to transform a simple XML string using XSL? -AC
7
398
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 other country. When I retrieve the phone numbers, I need to display them as (###) ###-#### x 99999 if it is a Canadian number or (###) ###-#### Ext. 99999 if it is US phone number. Potentially I'd have other countries added as well which might have...
3
4492
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/ HTML 4.01 spec: http://www.w3.org/TR/html401/ validator: http://validator.w3.org/ CSS 2.1 spec: http://www.w3.org/TR/CSS21/ validator: http://jigsaw.w3.org/css-validator/
7
3120
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
4363
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 unformatted data values (as decimals) just fine, so I know there's not a problem with the data retrieval, just the formatting. I have read that this would work: lblPrice.Text = prodRow.ToString("C");
1
8319
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 string str; str << ... << ...; SomeAPI( str.c_str() );
0
9727
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7669
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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 we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.