Why doesn't just a variable name display the variable in a script, like in the interpreter? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
开发者_StackOverflow社区 Improve this questionI want to display a string which is return
ed by a function in a Python script, without using print
:
def myfunc(mystring):
return "Converting to lowercase :{0}".format(mystring.lower())
result=myfunc("LOWER")
result
But it doesn't give an output. How can I get result
to display without print
?
Your code does print 'Converting to lowercase :lower'
when run in an interactive session (using python
or ipython
).
It does not, however, print anything if run as part of a script, since there is no implicit printing of expression results.
This is how it's meant to be.
If you want to print something from a script, use print
or sys.stdout.write()
.
An alternative to print:
sys.stdout.write()
精彩评论