Python (Scripting) - Return value in a function

by
Jeremy Canfield |
Updated: January 06 2024
| Python (Scripting) articles
If you are not familiar with functions, refer to Getting Started with functions in Python.
Let's say you have a function named "Hello". Here is how you would return "Hello World" when the Hello function is called.
#!/usr/bin/python
def Hello():
return "Hello World"
print Hello()
Or like this.
#!/usr/bin/python
def Hello():
return "Hello World"
result = Hello()
print(result)
In this example, running the Python script should print "Hello World".
~]$ python testing.py
Hello World
More commonly, either a dictionary or JSON is returned. For example, to return a dictionary.
#!/usr/bin/python3
def Hello():
return {"foo":"bar"}
result = Hello()
print(type(result))
print(result['foo'])
Will print the following.
<class 'dict'>
bar
Did you find this article helpful?
If so, consider buying me a coffee over at