The question appears to be asking about accessing the function's namespace, not just printing the value of the variables. If the namespace concept is new to you, I recommend reading the Python documentation and Real Python's blog post on Namespace's in Python. Let's look at a few ways to do what you are asking.

Printing the values is straightforward:

def times(value, power):
    print(f"Value passed to function was:  {value}")
    print(f"Power passed to function was:  {power}")
    print(f'Result of calculation is: {x: .6f}')

If you need to print it out the way you describe in your question, the values should be returned. This can be accomplished by updating your function to:

def times(value, power):
    return value, power, value**power

v, p, result = times(2,3)

print(f'Result of calculation is: {result: .6f}')
print(f"Value passed to function was:  {v}")
print(f"Power passed to function was:  {p}")

However, returning parameters seems a little odd since one would assume you as the developer can capture those values elsewhere in your code. If you want to view the variables and their values for a given namespace, use the corresponding function. For viewing the value and power variables, which live in the function times() local namespace, use locals() which returns a dictionary object that is a copy of the current local namespace.

def times(value, power):
    print(locals())
    return value**power

>>> times(5, 4)
{'value': 5, 'power': 4}
625

If the variables are defined in the global namespace, (keep in mind global variables should be used with care) you can use globals() to look up the value in the global namespace:

VALUE = 2
POWER = 3
def times(value=VALUE, power=POWER):
    return value**power

>>> globals()['VALUE']
2
>>> globals()['POWER']
3

I hope this helps you figure out how to accomplish what you are working on. I recommend taking some time to read about how Python views and manages namespaces. If you want to watch a video, check out this PyCon talk by Raymond Hettinger on object oriented programming 4 different ways.

Answer from Nathan on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-file-parameter-print
file parameter of Python's print() Function - GeeksforGeeks
October 27, 2023 - print() function in Python3 supports a โ€˜fileโ€˜ argument, which specifies where the function should write a given object(s) to. If not specified explicitly, it is sys.stdout by default. ... Note: The 'file' parameter is found only in Python 3.x or later.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-print-function
Python print() function - GeeksforGeeks
[1, 2, 3] ('A', 'B') Python ยท ... whether to flush the stream immediately (default is False) Note: The parameters sep, end, file, and flush in the print() function are keyword-only arguments....
Published ย  4 weeks ago
Discussions

python - How to print the values of parameters passed into a function - Stack Overflow
I donโ€™t know how to retrieve, store and print the values of parameters passed into a function. I do know that many posts are related to this question, but I couldn't find anything that matches the ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How can I print multiple things (fixed text and/or variable values) on the same line, all at once? - Stack Overflow
If you don't want spaces to be inserted automatically by print in the above example, change the sep parameter: print("Total score for ", name, " is ", score, sep='') If you're using Python 2, won't be able to use the last two because print isn't a function in Python 2. More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 7, 2018
How can I print all arguments passed to a python script? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
Getting list of parameter names inside python function - Stack Overflow
Is there an easy way to be inside a python function and get a list of the parameter names? ... Well we don't actually need inspect here. >>> func = lambda x, y: (x, y) >>> >>> func.__code__.co_argcount 2 >>> func.__code__.co_varnames ('x', 'y') >>> >>> def func2(x,y=3): ... print(func2.__c... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Where can I find a comprehensive list of ALL print() arguments? - Python Help - Discussions on Python.org
September 16, 2022 - Where is the official definition of all the functions built into Python, including all arguments and what they do? Specifically, I would like to know all available options for the print() function using sys.stdout. There seems to be no official definition of the print function, and only a general ...
Top answer
1 of 4
4

The question appears to be asking about accessing the function's namespace, not just printing the value of the variables. If the namespace concept is new to you, I recommend reading the Python documentation and Real Python's blog post on Namespace's in Python. Let's look at a few ways to do what you are asking.

Printing the values is straightforward:

def times(value, power):
    print(f"Value passed to function was:  {value}")
    print(f"Power passed to function was:  {power}")
    print(f'Result of calculation is: {x: .6f}')

If you need to print it out the way you describe in your question, the values should be returned. This can be accomplished by updating your function to:

def times(value, power):
    return value, power, value**power

v, p, result = times(2,3)

print(f'Result of calculation is: {result: .6f}')
print(f"Value passed to function was:  {v}")
print(f"Power passed to function was:  {p}")

However, returning parameters seems a little odd since one would assume you as the developer can capture those values elsewhere in your code. If you want to view the variables and their values for a given namespace, use the corresponding function. For viewing the value and power variables, which live in the function times() local namespace, use locals() which returns a dictionary object that is a copy of the current local namespace.

def times(value, power):
    print(locals())
    return value**power

>>> times(5, 4)
{'value': 5, 'power': 4}
625

If the variables are defined in the global namespace, (keep in mind global variables should be used with care) you can use globals() to look up the value in the global namespace:

VALUE = 2
POWER = 3
def times(value=VALUE, power=POWER):
    return value**power

>>> globals()['VALUE']
2
>>> globals()['POWER']
3

I hope this helps you figure out how to accomplish what you are working on. I recommend taking some time to read about how Python views and manages namespaces. If you want to watch a video, check out this PyCon talk by Raymond Hettinger on object oriented programming 4 different ways.

2 of 4
0

You will need to first store the parameters in variables in the code that calls the function.

Assuming the function 'times' is defined.

a = 2.72
b = 3.1
x = times(a, b)
print(f'Result of calculation is: {x: .6f}')
print(f'Value passed to function was: {a}')
print(f'Power passed to function was: {b}')
๐ŸŒ
Real Python
realpython.com โ€บ python-print
Your Guide to the Python print() Function โ€“ Real Python
June 25, 2025 - In contrast, Pythonโ€™s print() function always adds \n without asking, because thatโ€™s what you typically want. To disable it, you can take advantage of yet another keyword argument, end, which dictates what to end the line with. In terms of semantics, the end parameter is almost identical to the sep one that you saw earlier:
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_print.asp
Python print() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The print() function prints the specified message to the screen, or other standard output device.
๐ŸŒ
Codingem
codingem.com โ€บ home โ€บ python print() function parameters explained (a complete guide)
Python print() Function Parameters Explained (A Complete Guide) - codingem.com
March 23, 2023 - Python's built-in print() function takes one mandatory and four optional parameters. These parameters are *objects, sep, end, file, flush.
Find elsewhere
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ python tutorial โ€บ print statement in python
Print Statement in Python | Examples and Parameters of Print Statement
April 15, 2023 - space, so there is space between the different objects, and the end parameter has a value โ€œ!!!โ€ so the string ends with โ€œ!!!โ€. ... string1 = "Hi " string2 = "Hello " string3 = "World" string4 = "!!!" print(string1+string2+string3+string4)
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-print.html
print() and Standard Out
The Python print() function takes in any number of parameters, and prints them out on one line of text. The items are each converted to text form, separated by spaces, and there is a single '\n' at the end (the "newline" char). When called with zero parameters, print() just prints the '\n' ...
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ print() function in python with all parameters (+examples)
Print() Function In Python With All Parameters (+Examples)
February 3, 2025 - Internally, print() takes the objects ... When multiple arguments are provided, they are separated by the sep parameter, which defaults to a single space....
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ print
Python print()
print("Python is fun.") a = 5 # Two objects are passed ... Python is fun. a = 5 a = 5 = b ยท In the above program, only the objects parameter is passed to print() function (in all three print statements).
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ print-function
Explore Python's print() function, master output customization
Pythonโ€™s print() function takes several arguments, including objects to output and various optional arguments. ... objects: One or more expressions or objects to output to the console. These can include arrays or other iterable types. sep: The optional sep parameter specifies the separator ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-print-multiple-arguments-in-python-418811
How to print multiple arguments in Python | LabEx
It provides a simple and versatile ... straightforward: print(object(s), sep=' ', end='\n') Key parameters include: object(s): The items you want to print ยท...
Top answer
1 of 14
699

There are many ways to do this. To fix your current code using %-formatting, you need to pass in a tuple:

  1. Pass it as a tuple:

    print("Total score for %s is %s" % (name, score))
    

A tuple with a single element looks like ('this',).

Here are some other common ways of doing it:

  1. Pass it as a dictionary:

    print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
    

There's also new-style string formatting, which might be a little easier to read:

  1. Use new-style string formatting:

    print("Total score for {} is {}".format(name, score))
    
  2. Use new-style string formatting with numbers (useful for reordering or printing the same one multiple times):

    print("Total score for {0} is {1}".format(name, score))
    
  3. Use new-style string formatting with explicit names:

    print("Total score for {n} is {s}".format(n=name, s=score))
    
  4. Concatenate strings:

    print("Total score for " + str(name) + " is " + str(score))
    

The clearest two, in my opinion:

  1. Just pass the values as parameters:

    print("Total score for", name, "is", score)
    

    If you don't want spaces to be inserted automatically by print in the above example, change the sep parameter:

    print("Total score for ", name, " is ", score, sep='')
    

    If you're using Python 2, won't be able to use the last two because print isn't a function in Python 2. You can, however, import this behavior from __future__:

    from __future__ import print_function
    
  2. Use the new f-string formatting in Python 3.6:

    print(f'Total score for {name} is {score}')
    
2 of 14
64

There are many ways to print that.

Let's have a look with another example.

a = 10
b = 20
c = a + b

#Normal string concatenation
print("sum of", a , "and" , b , "is" , c) 

#convert variable into str
print("sum of " + str(a) + " and " + str(b) + " is " + str(c)) 

# if you want to print in tuple way
print("Sum of %s and %s is %s: " %(a,b,c))  

#New style string formatting
print("sum of {} and {} is {}".format(a,b,c)) 

#in case you want to use repr()
print("sum of " + repr(a) + " and " + repr(b) + " is " + repr(c))

EDIT :

#New f-string formatting from Python 3.6:
print(f'Sum of {a} and {b} is {c}')
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ python-print-function
A Complete Guide to the Python print() Function | LearnPython.com
January 25, 2022 - It defaults to the newline character ("\n"). None of the arguments of the Python print() function are required, strictly speaking. If no arguments are given, the end keyword is printed, which is the newline character by default.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ inputoutput.html
7. Input and Output โ€” Python 3.14.3 documentation
>>> animals = 'eels' >>> print(f'My hovercraft is full of {animals}.') My hovercraft is full of eels.
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ print-in-python
Python print() Function - Scaler Topics
December 20, 2021 - sep, end, file, and flush are keyword arguments and are optional parameters in print() in Python.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-print-function
Python print Function
March 31, 2025 - The range function default uses the new line as the end argument value. However, you can override it. In the above print function example, the two statements display the strings in two different lines. Here, we used the end parameter with space as the value and the second statement with .