473,320 Members | 1,921 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,320 software developers and data experts.

forced spaces when inserting a variable between strings

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 :-)
--
m j teigen

Jan 17 '06 #1
3 1543
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.

Jan 17 '06 #2
mjteigen wrote:
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/...q-strings.html
http://diveintopython.org/native_dat...g_strings.html
Jan 17 '06 #3
mjteigen wrote:
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 :-)
--
m j teigen

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)
Jan 17 '06 #4

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

Similar topics

6
by: Tim J. | last post by:
When I send a message using the mail command in PHP some e-mail addresses somehow put two returns in place of one return, so this: $message = "Test Test2" becomes Test
9
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test ...
4
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely....
2
by: Bill Mittenzwey | last post by:
Is there a way to do a Regex replace on a string to stuff some variable amount of spaces into a location of a string? I need an expression which would take an unknown length string and move the...
2
by: Alan Silver | last post by:
Hello, VWD Express seems obsessed with inserting loads of spaces at the start of each line whenever you drag controls into the source view. I guess this is MS' idea of making the source code...
17
by: tommy | last post by:
Hi all, I' m adding strings to some fields in my table via Access. The strings sometimes have trailing spaces and I really need to have it that way, but Access truncates trailing spaces. How can...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
8
by: starsky51 | last post by:
I'm sure it's something i'm doing wrong, I just can't see it. I've set up a simple page with the following code: <html> <head> <title>tester</title> <script language="javascript"...
0
by: RobcPettit | last post by:
Hi, Ive been breaking down my string with regex and havnt been doing to bad for a begginer. However Im struggling now. Examole string = '10 04 thismaybemissing 10 rob 1 2 3 robagain 3 74 * 70 4 2'...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.