Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

I want to write a printf() function, similar to the one that C has, in Python. Here is the straightforward way to do it: def printf(format, *args): print format % (args), The problem is that the print, statement (including the comma) leaves a trailing whitespace character after printing something. This makes my Python implementation of printf work even more differently from the one in C: # Python >>> printf("Hello"); printf("World!") >>> Hello, World! /* C */ #include main() { printf("Hello"); printf("World!"); } Output: HelloWorld!

OpenStudy (anonymous):

How do I fix this?

OpenStudy (anonymous):

hmm... it's missing <stdio.h> in the C example O.o

OpenStudy (anonymous):

#include <stdio.h>

OpenStudy (anonymous):

how come?

OpenStudy (anonymous):

I recall typing it in!

OpenStudy (wasiqss):

#include<conio.h>

OpenStudy (wasiqss):

instead of main write void main(void)

OpenStudy (anonymous):

it works when you type it an 'answer' box, but not when it's inside the initial question box.

OpenStudy (anonymous):

I know my C example is not standard C :-P but it's just supposed to illustrate the problem.

OpenStudy (anonymous):

cmon, Python pros out there! a printf() would be useful in python.

OpenStudy (anonymous):

thanks

OpenStudy (anonymous):

How do I write a printf function using sys.stdout.write?

OpenStudy (anonymous):

I mean, how do I do formatting?

OpenStudy (anonymous):

In python we can simulate C++ 's 'cout' thing http://ideone.com/NkJgt

OpenStudy (anonymous):

In python the % operator formats a string with the replacement parameters, and returns the resulting string. Just because you normally do it as part of a print statement doesn't limit the behavior to printing. >>> s = "%s, %s!" % ("Hello", "World") >>> s 'Hello, World!' No printing necessary.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!