Reputation: 137
I am wondering if there is a way to return more than one value and space it out between 2 or more lines. My current example code:
def exampleReturn():
x = 'Hello'
y = 'World'
return (x,y) #('Hello', 'World')
Currently, this just returns x and y in a tuple. However, I am wondering if there is a way to return these two values on two lines like this:
exampleReturn()
>>>'Hello'
'World'
Like the above example, I would like for them to be on separate lines as strings instead of tuples, and with both lines starting at the same position.
Upvotes: 0
Views: 8563
Reputation: 133
The simple and best way is to return that function into a list or something and then, try to print the list however you want
Upvotes: 0
Reputation: 3405
There are two kinds of output that you see in the interactive prompt corresponding to
obj.__repr__()
returns (when you just press the Enter)stdout
, whatever obj.__str__()
returns(when you call print(obj)
)In your problem what you are seeing is the 1st one. If you want a newline displayed, you are concerned about print()
ing. There are many ways to do this. One possibility is,
In [453]: x = exampleReturn()
In [454]: print(*x,sep="\n")
hello
world
Upvotes: 1
Reputation: 4010
Keep returning a tuple (just the data); it's a better design choice than doing print formatting in exampleReturn()
because it allows you to change the print format later on. Then, elsewhere, simply do:
print("\n".join(exampleReturn()))
If exampleReturn()
returns numbers rather than strings, do:
print("\n".join([str(x) for x in exampleReturn()]))
Upvotes: 1
Reputation: 387547
You have a fundamental misunderstanding of what return values are. A return value is something that is returned from a function; it can be any kind of object: A string, an integer, a tuple, a list, some complex object. It doesn’t matter.
Return values are usually never displayed. They are there for you to capture in variables.
For example str.split()
splits a string and returns a list of parts. If you use it like this in a Python script, you don’t get anything:
'foo,bar'.split(',')
The return value is just thrown away. And the visual output from this script would be empty.
In order to do something with return values, you need to capture them in a variable, or pass them to something else directly:
x = 'foo,bar'.split(',')
This would capture the returned list in the variable x
which you could then use.
If you want to display a return value, you need to print it:
x = 'foo,bar'.split(',')
print(x)
# or directly:
print('foo,bar'.split(','))
That’s all there is to return values.
Now, in an interactive console, a REPL, in order to get better feedback, the consoles will usually display the return values of things you execute (unless the return value is None
). That’s why in an interactive Python console, you get the following output:
>>> 'foo,bar'.split(',')
['foo', 'bar']
That ['foo', 'bar']
is the return value of the split
call, so it’s not automatically displayed. The interactive console will take the otherwise thrown-away return value and display it. But that’s entirely a feature from the interactive console, and not something we can affect. We cannot even rely on it to happen.
If you want a certain output, then you should print it yourself:
>>> x = 'foo,bar'.split(',')
>>> print('\n'.join(x))
foo
bar
Upvotes: 5