8

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?

3
  • 2
    Not surprising that you get an error, since the syntax is so wrong. Commented Jan 12, 2011 at 14:21
  • @S.Lott The question is pretty much useless now. I cannot delete it since it has answers. Commented Jan 12, 2011 at 14:26
  • 3
    Not utterly useless -- there's a possibility that other people may have the same syntax question. Commented Jan 12, 2011 at 14:35

1 Answer 1

16

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.