🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

python print

Started by
9 comments, last by nano 21 years ago
Is it possible to print a block of embedded text in python? In perl you can do it like this this:
print <<EOF;
some text..
and maybe a $var here
EOF 
[Edit: The forum interprets a '<' as opening an HTML tag, so it will swallow any text you have in there. Use &lt; instead. Also, to get code formatting use either the source or code tags. Read the site FAQ for more info.] [edited by - Oluseyi on June 7, 2003 3:50:18 PM]
Advertisement
Not sure I am following. Can you give a more concrete example with input/output? I just don''t get exactly what you are trying to do.

In python you can do:

TestVar = 5
print ''Value is '' + str(TestVar) + '' do da do dah''

or:

print (''Value is %d do da do dah'') % (TestVar)

What is it that you are trying to do?
If I want to output a piece of html from my script without getting it from some external source, but instead have it embedded in my script I want to avoid having to quote every line of html.
I could do:

print 'html..'
print 'html..'
...

In perl I can do:

print <<EOF;
html..
html..
...
EOF

Quite nice, because then you can just copy and paste the html there without having to print and quote every line.

[Edit: Forum, HTML...]

[edited by - Oluseyi on June 7, 2003 3:59:19 PM]
You can''t do that in Python. Perl evolved from the position of string processing first into a full programming language, so its string processing capabilities are exceptionally strong. PHP can also do the block text thing. In Python you''d need to do a couple of things, given that it''s print functionality is more C-like:
# Python:print ''some text '', var, '' and more text.\n''print ''text that starts on one line...\n'' \      ''requires a continuation character...\n'' \      ''and an explicit newline for the ouput to break.'' 
Uhm, no.


print """some text, %(var)s and more text.
text that starts on one line...
does not require a continuation character and no
explicit newline for the ouput to break.""" % locals()

The """ is the trick.

(the example above assumes that var is a local variable. If not, use %s and an ordered sequence(tuple, list) instead.)


AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
That''s interesting.. I''ll try that.
Well, I tried it but got a little dissapointed.
Apparently when doing formatting, some characters become
invalid. Those include " > < and obviously those are
important in html. So what I had to do is copy and paste
the html and then, where I would have formatting variables,
instead write something like !var1!. Then do a search replace
and print:

s = """<tag>
<tag2 = "!var1!">
<tag3 = "!var2!">
</tag>"""
print s.replace('!var1!',var1).replace('!var2!',var2)

Oh well...



[edited by - nano on June 7, 2003 7:50:54 PM]
Uh, what are you talking about? The following works perfectly for me:

title = "Hello world"print """<html><head>    <title>%(title)s</title></head><body>    <h1>%(title)s</h1></body></html>""" % locals()


Output:
>pythonw -u test.py<html><head>    <title>Hello world</title></head><body>    <h1>Hello world</h1></body></html>>Exit code: 0



AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
I got errors that were telling me that thoe characters were invalid, but I guess someinth else caused it. The percent character is however causing problems, so I have to change
all % as %%.

I''m getting sloppy.. No, wait.. I''ve always been sloppy..
quote: Original post by Arild Fines
Uhm, no.
Wow, I thought the triple-quoted strings were only useful as docstrings. I never saw anything like this in the documentation. Thanks!

This topic is closed to new replies.

Advertisement