Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed variable using sys.stdout.write in Python

I can embed variables using the print statement in python in this way

i=10
print "Value is %s" % (i)

Output

Value is 10

but doing this

i=10
sys.stdout.write ("Value is %s") % (i)

gives me the following error

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

Can I embed variables using sys.stdout.write instead of print?

like image 778
abel Avatar asked Jan 12 '11 14:01

abel


1 Answers

You got the parentheses wrong. Should be

i=10
sys.stdout.write("Value is %s" % i)

The % operator takes a string and a tuple (or a single object) as arguments. You tried to apply the operator to the return value of sys.stdout.write(), which is None. You need to apply it to the string before it is passed to sys.stdout.write().

like image 88
Sven Marnach Avatar answered Oct 29 '22 03:10

Sven Marnach