forced spaces when inserting a variable between strings

M

mjteigen

I'm very new at Python, but have been trying it in conjunction with
CGI. I've encountered a problem that I'm pretty sure is a trivial one,
but I don't know enough Python to work it out. Here's an example.
Imagine that I have a file named "5.jpg" in the same directory as this
Python script:

print "content-type: text/html\n"
number = 5
print "<img src=",number,".jpg>"

My goal is print out '<img src="5.jpg">'. However, when I view the
source on the generated html page, I see this:

<img src= 5 .jpg>

In other words, that "5" has a space tacked on either side of it, and
of course a browser can't find the file. Is there a way I can avoid the
tacking of spaces on either side of a variable inserted between two
strings?

TIA :)
 
S

snoe

You can use:

print "<img src=%s.jpg>" % (number)
or
print "<img src="+str(number)+".jpg>"

or a number of others, but for short strings one of these two generally
work fine.
 
S

Stephen Illingworth

mjteigen said:
My goal is print out '<img src="5.jpg">'. However, when I view the
source on the generated html page, I see this:

<img src= 5 .jpg>

In other words, that "5" has a space tacked on either side of it, and
of course a browser can't find the file. Is there a way I can avoid the
tacking of spaces on either side of a variable inserted between two
strings?

Yes, use placeholders. In your example,

print "<img src=\"%d.jpg\">" % (number,)

http://www.python.org/dev/doc/devel/lib/typesseq-strings.html
http://diveintopython.org/native_data_types/formatting_strings.html
 
X

Xavier Morel

mjteigen said:
I'm very new at Python, but have been trying it in conjunction with
CGI. I've encountered a problem that I'm pretty sure is a trivial one,
but I don't know enough Python to work it out. Here's an example.
Imagine that I have a file named "5.jpg" in the same directory as this
Python script:

print "content-type: text/html\n"
number = 5
print "<img src=",number,".jpg>"

My goal is print out '<img src="5.jpg">'. However, when I view the
source on the generated html page, I see this:

<img src= 5 .jpg>

In other words, that "5" has a space tacked on either side of it, and
of course a browser can't find the file. Is there a way I can avoid the
tacking of spaces on either side of a variable inserted between two
strings?

TIA :)
1- Use sys.stdout.write(), it doesn't add any kind of formatting to the data
2- You may also use string interpolation, either in sys.stdout.write or
in print
3- I'd suggest you to use a templating system in order to separate the
displayed data and the retrieval/computations of said data, and prevent
from mixing logic and presentation in your scripts (which always leads
to an unmaintainable system)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top