Python Forum

Full Version: Assign dynamic values to Variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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'}   
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
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
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.
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.