Are there sprintf in Python???

Q

questions?

Are there any sprintf in Python?
I know you can print to files(or redefine sys.stout) and later open the
file content.

Are there similar function to sprintf in C?

Thanks
 
R

rzed

Are there any sprintf in Python?
I know you can print to files(or redefine sys.stout) and later
open the file content.

Are there similar function to sprintf in C?

Something like this?

x = 9
vbl = "One digit: %d, four digits: %04d" % (x,x)
print vbl
 
N

Nanjundi

Are there any sprintf in Python?

Refer module StringIO - just like file input/output operations.
cStringIO is another module (faster)

Quick intro:
from StringIO import StringIO
s = StringIO()
s.write('hello')
s.seek(0)
print s.read()

-N
 
N

Neil Cerutti

Are there any sprintf in Python?
I know you can print to files(or redefine sys.stout) and later open the
file content.

Are there similar function to sprintf in C?

No, but you can compose a similar function very easily.

def sprintf(format, *objects):
return format % tuple(objects)

Since strings are immutable it's not convenient to provide a
pass-out parameter as in C. If you want to check for errors
you'll have to catch the exceptions, rather than inspect the
return value as you can in C.
 
F

Facundo Batista

questions? wrote:

Are there similar function to sprintf in C?

Meaning to print in a buffer? It's not necessary...

Remember that all the ways that prints on files, actually does not need
to print into *actual* files, but they can print into file-like objects
(see StringIO, or mmap, for example).

Regards,
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top