Python Forum
Assign dynamic values to Variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assign dynamic values to Variables
#1
Hi,

In python I am trying to assign username and password(with out hard coding). NameVALUE and PasswordVALUE variables have values

paramvalue = "{\"username\": \"NameVALUE",\"password\": \"PasswordVALUE\"}"  
When I print paramvalue I am getting out put like
Output:
{\"username\": \"NameVALUE",\"password\": \"PasswordVALUE\"}
expected output:
Output:
{\"username\": \"Srikanth",\"password\": \"Welcome\"}
Please help.Thank you in advance.

Regards
Srikanth
Reply
#2
If this is JSON, you could just use a dictionary (rather than a string) and produce a JSON string using the json module. If you really insist on using strings, then you can use a format string of course.
Reply
#3
What exactly is the problem? If you assing string as value it will be string. If you assign variable, it will be variable value:

>>> ham = 'ham'                                                                                                       
>>> spam = 'spam'                                                                                                     
>>> d = {'first': ham, 'second': spam}                                                                                
>>> d                                                                                                                 
{'first': 'ham', 'second': 'spam'} 
>>> d = {'first': 'nameValue', 'second': 'passwordVALUE'}
>>> d                                                                                                                 
{'first': 'nameValue', 'second': 'passwordVALUE'}   
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
When you say you are trying to "assign" username and password, do you mean you intend for those to be variables? Or are you trying to use string formatting like the example below?
NameVALUE = 'Dave'
PasswordVALUE = 'swordfish'
paramvalue = f'Username: {NameVALUE}, Password: {PasswordVALUE}'
print(paramvalue)
Output:
Username: Dave, Password: swordfish
Reply
#5
Hi ndc85430, perfringo,

Thank you for responding.

@ndc85430,

I am using dictionary it is working but I am getting in Single quotes as shown below
{'username': 'Domain\\Srikanth', 'password': 'Hello123'}

I need output in as shown below:
{"username": "Domain\\Srikanth","password": "Hello123"}

Please let me know. Thank you.
Regards
Srikanth

(Jun-06-2020, 01:29 PM)ndc85430 Wrote: If this is JSON, you could just use a dictionary (rather than a string) and produce a JSON string using the json module. If you really insist on using strings, then you can use a format string of course.

Hi, ndc85430,

Thank you for responding

I am using dictionary it is working but I am getting in Single quotes as shown below
{'username': 'Domain\\Srikanth', 'password': 'Hello123'}

I need output in as shown below:
{"username": "Domain\\Srikanth","password": "Hello123"}

Please let me know. Thank you.
Regards
Srikanth
Reply
#6
Ah, I see that I completely misunderstood your intent. Smile

The single quotes are there because that's how Python shows that the values inside the quotes are strings. As ndc85430 indicated, you could use the json module if you need JSON output.
import json
d = {"username": "Domain\\Srikanth","password": "Hello123"}
print(d)
print(json.dumps(d))
Output:
{'username': 'Domain\\Srikanth', 'password': 'Hello123'} {"username": "Domain\\Srikanth", "password": "Hello123"}
I apologize if I'm still misunderstanding what you're trying to do here.
Reply
#7
Thank you very much GOTO10. Its perfectly worked. Thanks a lot :)

(Jun-06-2020, 02:59 PM)GOTO10 Wrote: Ah, I see that I completely misunderstood your intent. Smile

The single quotes are there because that's how Python shows that the values inside the quotes are strings. As ndc85430 indicated, you could use the json module if you need JSON output.
import json
d = {"username": "Domain\\Srikanth","password": "Hello123"}
print(d)
print(json.dumps(d))
Output:
{'username': 'Domain\\Srikanth', 'password': 'Hello123'} {"username": "Domain\\Srikanth", "password": "Hello123"}
I apologize if I'm still misunderstanding what you're trying to do here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 284 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Create X Number of Variables and Assign Data RockBlok 8 954 Nov-14-2023, 08:46 AM
Last Post: perfringo
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,492 Jul-27-2022, 08:50 PM
Last Post: rob101
  Create array of values from 2 variables paulo79 1 1,092 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  How can I assign "multiple variables" to a single "value"? Psycpus 2 1,860 Oct-04-2021, 03:29 AM
Last Post: deanhystad
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 1,937 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Giving all possible values to four different variables quest_ 7 2,966 Jan-18-2021, 05:18 AM
Last Post: deanhystad
  Variables being overridden to initial values. p2bc 6 2,635 Oct-10-2020, 09:03 PM
Last Post: p2bc
  Print variable values from a list of variables xnightwingx 3 2,640 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Create, assign and print variables in loop steven_tr 10 4,356 May-28-2020, 04:26 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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