Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print variable name?
#7
(Oct-18-2019, 12:29 PM)perfringo Wrote:
>>> x, y = 7, 42
>>> name_x_as_string = f'{x=}'.split('=')[0]
>>> name_y_as_string = f'{y=}'.split('=')[0]
>>> f'The length of {name_x_as_string}{name_y_as_string} is {x + y}'
'The length of xy is 49'

It might be something like this.

But still I need it to be in function. I want to call the function with variables like this:

def length(x,y):
    name_x_as_string = f'{x=}'.split('=')[0]
    name_y_as_string = f'{y=}'.split('=')[0]
    print(f'The length of {name_x_as_string}{name_y_as_string} is {x + y}')

a=5
b=3
length(a,b)
The result here is:

The length of xy is 8
It's not what I need. The result should be:

The length of ab is 8
Is it understandable?
Reply


Messages In This Thread
How to print variable name? - by mayadob - Oct-18-2019, 10:11 AM
RE: How to print variable name? - by mayadob - Oct-18-2019, 10:33 AM
RE: How to print variable name? - by Larz60+ - Oct-18-2019, 11:08 AM
RE: How to print variable name? - by mayadob - Oct-18-2019, 11:23 AM
RE: How to print variable name? - by perfringo - Oct-18-2019, 12:29 PM
RE: How to print variable name? - by mayadob - Oct-20-2019, 01:10 PM
RE: How to print variable name? - by mayadob - Oct-20-2019, 04:07 PM
RE: How to print variable name? - by mayadob - Oct-18-2019, 02:28 PM
RE: How to print variable name? - by ichabod801 - Oct-20-2019, 05:45 PM
RE: How to print variable name? - by ichabod801 - Oct-21-2019, 01:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Print variable without '' and spaces arnonim 1 757 Jan-30-2023, 05:23 PM
Last Post: deanhystad
  Problem with print variable in print.cell (fpdf) muconi 0 695 Dec-25-2022, 02:24 PM
Last Post: muconi
  Remove a space between a string and variable in print sie 5 1,871 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 2,009 Apr-30-2022, 11:48 PM
Last Post: hammer
  Print variable between " paulo79 5 1,767 Mar-11-2022, 05:16 PM
Last Post: deanhystad
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 2,404 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov
  Print variable values from a list of variables xnightwingx 3 2,681 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Print part of a variable rs74 4 2,638 Jul-27-2020, 04:39 PM
Last Post: rs74
  How do I print a returned variable calculated in another function? RedSkeleton007 3 3,596 Jul-10-2018, 12:10 PM
Last Post: buran
  Empty variable when using print before readline fstefanov 3 3,708 Oct-23-2017, 02:22 AM
Last Post: fstefanov

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020