473,785 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another n00b: Removing the space in "print 'text', var"

More of a minor niggle than anything but how would I remove the
aforementioned space?

eg.
strName = 'World'
print 'Hello', strName, ', how are you today?'

comes out as "Hello World , how are you today?"

Have googled, and worked my way through the first 7 chapters of Byte of
Python, but to no avail...

TIA

HH
Feb 12 '06 #1
6 1508
HappyHippy <no*@bleedin.li kely> wrote:
More of a minor niggle than anything but how would I remove the
aforementioned space?

eg.
strName = 'World'
print 'Hello', strName, ', how are you today?'

comes out as "Hello World , how are you today?"

Have googled, and worked my way through the first 7 chapters of Byte of
Python, but to no avail...


Use string formatting:

print 'Hello %s, how are you today?' % strName
Alex
Feb 12 '06 #2
"HappyHippy " wrote:
More of a minor niggle than anything but how would I remove the
aforementioned space?

eg.
strName = 'World'
print 'Hello', strName, ', how are you today?'

comes out as "Hello World , how are you today?"

Have googled, and worked my way through the first 7 chapters of Byte of
Python, but to no avail...


you can concatenate the strings before passing them to print:

text = 'Hello' + strName + ', how are you today?'
print text

or do the same on one line

print 'Hello' + strName + ', how are you today?'

or use string formatting (the % operator), or write to the special sys.stdout
file object (output to files is discussed in chapter 12).

</F>

Feb 12 '06 #3
Em Dom, 2006-02-12 Ã*s 22:11 +0000, HappyHippy escreveu:
More of a minor niggle than anything but how would I remove the
aforementioned space?

eg.
strName = 'World'
print 'Hello', strName, ', how are you today?'

comes out as "Hello World , how are you today?"


strname = 'World'
print 'Hello %s, how are you today?' % strname

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação' . Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"

Feb 12 '06 #4
HappyHippy wrote:
More of a minor niggle than anything but how would I remove the
aforementioned space?
eg.
strName = 'World'
print 'Hello', strName, ', how are you today?'

Already got an anwser, now just a coding-style suggestion: hungarian
notation is *evil*. And even *more* evil with a dynamically-typed language.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Feb 13 '06 #5
bruno at modulix <on***@xiludom. gro> wrote in
news:43******** **************@ news.free.fr:
HappyHippy wrote:
More of a minor niggle than anything but how would I remove the
aforementioned space?


eg.
strName = 'World'
print 'Hello', strName, ', how are you today?'

Already got an anwser, now just a coding-style suggestion: hungarian
notation is *evil*. And even *more* evil with a dynamically-typed
language.


Bruno -

<OT>
Although I agree with you in part (about the horror that is often called
"Hungarian notation"), I would have to say that this particular use of
it does not demonstrate that evil. What I consider bad about it is that
people include an indication of datatype in the name, so that, say, the
name of a C int would begin with the letter 'i'. The evil comes about
when a different algorithm causes a change in the datatype of the
variable (it is, for instance, redefined as a long, or a char), but the
name itself does not change. From that point on, when encountering a
variable whose name begins with 'i', it's not possible to be sure that
it is an int, regardless of the coding convention involved.

However, that is not how Hungarian notation was meant to be used.
Instead, the prefix was to indicate the function of the variable, not
its datatype. Thus, ctrXXX would be a counter (according to some
hypothetical coding standard). If the datatype changed, the name would
not have to change, as long as the variable's function remained the
same.

In the HappyHippy example, the 'str' prefix does coincide with Python's
indicator of a string class, as it happens. It is not clear that it need
be more than coincidence, though. A string of bytes need not be of class
str, and text need not be contained in a Python str, but the 'str'
prefix could easily be attached to anything that indicated strung-
together data under some coding standard or another. Specifically, it
wouldn't bother me greatly to discover that strXXX contained a normal
Python string at some point and a Unicode string at another -- the
function of strXXX would be the same in either case.

Dealing with the contents of strXXX might be a problem, of course, but
that would also be true whenever the datatype of its contents could
vary, regardless of its name. In a dynamic language, that variance could
easily happen, which appears to be the basis for your "even *more*
evil" comment. Appending a pseudotype prefix to an object's name doesn't
fix the situation, but it doesn't really make it worse. A prefix of
'str' should be enough to warn a maintainer not to assume the field is
an accumulator or a function name, and maybe a little more than that,
but in any event, in any language, the only sure way to know how an
object is being used is to examine its context in the source. Anything
naming convention (or comment, for that matter), is just dropping
crystallized hints that are relavent to some particular moment in time.
It is folly to assume they are necessarily still relevant.

Now it could be that I'm misinterpreting your objection to Hungarian
notation. If so, I'd be interested in hearing what it is. Although I
don't imagine any of this is actually on topic in the Python group.
</OT>

--
rzed
Feb 13 '06 #6
Rick Zantow a écrit :
bruno at modulix <on***@xiludom. gro> wrote in
news:43******** **************@ news.free.fr:

HappyHippy wrote:
(snip)
eg.
strName = 'World'
print 'Hello', strName, ', how are you today?'
Already got an anwser, now just a coding-style suggestion: hungarian
notation is *evil*. And even *more* evil with a dynamically-typed
language.


<OT>
Although I agree with you in part (about the horror that is often called
"Hungarian notation"),

(snip)
However, that is not how Hungarian notation was meant to be used.
Instead, the prefix was to indicate the function of the variable, not
its datatype.
I know that. But:
1/ 99% of the developpers using (ms-perverted) hungarian notation ignore
this fact
2/ the name itself should be a clear enough indication of the [role,
function, intended use] of the variable (<pedantic>well , of the object
bound to that name in fact</pedantic>)
3/ see below...
4/ see below...
Thus, ctrXXX would be a counter (according to some
hypothetical coding standard).
or a controller, according to some other hypothetical coding standard...

3/ prefixing (instead of suffixing) breaks readability.
compare :
- hit_counter
- hit_controller
- ctrHit

Which one do you find the most readable ? Clearly, prefixing (vs
suffixing) puts things in the wrong order. And clearly, a three letters
abbrev gives less information than a word.

(snip)
In the HappyHippy example, the 'str' prefix does coincide with Python's
indicator of a string class, as it happens.
as it happens... cf /1
It is not clear that it need
be more than coincidence, though.
Which, given /1, is enough to confuse the situation. Ho, my, does this
prefix means that it's supposed to be a string, or is it mere
coïncidence ? And if so, what is this 'str' prefix supposed to mean ?

So much for readability...
A string of bytes need not be of class
str, and text need not be contained in a Python str, but the 'str'
prefix could easily be attached to anything that indicated strung-
together data under some coding standard or another.

Specifically, it
wouldn't bother me greatly to discover that strXXX contained a normal
Python string at some point and a Unicode string at another -- the
function of strXXX would be the same in either case.

4/ Well, I'm afraid that 'strung-together data' is more a type
indication than a 'function' indication. And what should I say about
byte string or Unicode...

Dealing with the contents of strXXX might be a problem, of course, but
that would also be true whenever the datatype of its contents could
vary, regardless of its name. In a dynamic language, that variance could
easily happen, which appears to be the basis for your "even *more*
evil" comment.
Of course.
Appending a pseudotype prefix to an object's name doesn't
fix the situation, but it doesn't really make it worse.
Yes it does. How do you like:

strAnswer += 42

A prefix of
'str' should be enough to warn a maintainer not to assume the field is
an accumulator or a function name,
A maintainer should not assume much IMHO. Heck, a function *name* is a
string, isn't it ? And BTW, if, following some weird naming convention,
the 'str' prefix was used to indicate 'strung-together-data', it could
as well be used for any sequence(strung-together-data) - in which case
it could be used for an accumulator !-)
and maybe a little more than that,
but in any event, in any language, the only sure way to know how an
object is being used is to examine its context in the source.
So what's the use of prefixing it's name with distracting information ?

(snip)
Now it could be that I'm misinterpreting your objection to Hungarian
notation.
I'm afraid my objections to HN are nothing new.
If so, I'd be interested in hearing what it is.
Then you're done. To summarize:
- it's useless
- it's distracting
- it can be misleading
- it doesn't help readability

According to Python's zen:
- Beautiful is better than ugly.
- Readability counts.
- In the face of ambiguity, refuse the temptation to guess.
Although I
don't imagine any of this is actually on topic in the Python group.
oops ! Sorry y'all
</OT>


Feb 13 '06 #7

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

Similar topics

3
1827
by: Jacob Larsen | last post by:
Is <select name="var"> ok according to w3.org? Regards Jacob
3
3271
by: Silmar | last post by:
Hi! In my form I have table which cells contain input objects of type="text" which initially are disabled. I would like to activate them by clicking on them. However because input object does not support onClick event (or maybe I am wrong?) I use onClick events of table cells. And it works as required in IE 6. I click on the input object and the onClick event of the cell is raised. But it does not work in Firefox (I have not yet checked...
6
5266
by: Jon Davis | last post by:
I recently learned how to do an <OBJECT> alternative to <IFRAME> in current browsers using: <object id="extendedhtml" type="text/html" data="otherpage.html" width="250" height="400"></object> My question is how do I access the document DOM of this object in Javascript? For example, "alert(extendedhtml.innerHTML);" doesn't work and produces an unknown error. I'd like to both read and write to the document's body element's innerHTML...
16
660
by: John Baker | last post by:
HI; I feel like a fool..I put CUTE FTP in my last request for help --it should have been CUTE PDF! I have FTP on my mind because I have been working on a web based application, and somehow my brain got stuck on it..sorry! Hi: I need some help writing code that will "print" a report using Cute PDF.
2
3797
by: gabon | last post by:
I'm creating a select entirely through JavaScript and very strangely IE doesn't show the text in the option elements. Here part of the code: this.form_country=document.createElement("select"); var option; for(var i=0; i<arr.length; i++){ option=document.createElement("option"); option.value=arr.code;
1
2324
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php but now I am not sure if that makes sense. Can you tell me please ? <html> <head>
45
9136
by: Umesh | last post by:
please help. thanks.
10
1589
by: Prisoner at War | last post by:
Hi, your friendly neighborhood n00b here, just wondering why on earth the Py3K folks want to mess with a simple thing like the "print" "command" (is that what it's called, a command?), turning it into "print()"...I mean, what's the point, exactly?? To look like a more "traditional" computer-language format? And what's with not supporting the so-called softspace "feature" of the current "print" command, where a space after a comma, like
2
2459
by: sixtyfootersdude | last post by:
Good Morning! I am just starting to learn perl and I am somewhat mistifide about when I should do: print("@input"); and when I should do: print(@input)
0
10346
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
10157
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
10096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
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...
1
4055
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
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.