Python Forum
Add variable value to string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add variable value to string
#1
I am creating variable

dashboard_id="66862fa0-dafa-4d28572533a"
I want to create a another variable and insert this value to it

Output:
{ "objects":[ { "type":"dashboard", "id":"66862fa0-dafa-4d28572533a" } ] , "includeReferencesDeep": true }
this is how i am creating this but do not know how to replace this id

data = '\n{ "objects":[\n   {\n "type":"dashboard",\n "id":"66862fa0-dafa-4d28572533a"\n } ] ,\n "includeReferencesDeep": true\n}'
Reply
#2
Use and F-String.

id = "66862fa0-dafa-4d28572533a"

string = f"some stuff{id}some more stuff"
Reply
#3
I did do this and it worked. but now thinking if I can do this in one line

      data1 = '\n{ "objects":[\n   {\n "type":"dashboard",\n "id":'
      data2 = '\n } ] ,\n "includeReferencesDeep": true\n}'
      data = data1 + '"' + dashboard_id + '"' + data2

Michael, that does not work beceasue I have { and ' and \n all in that string. it gives me error
Reply
#4
You are creating JSON. Of course one way is to use string manipulation/str methods to do it.
Better way is to use json module
import json

data = {"objects":[],
        "includeReferencesDeep": True}

dashboard_id = "66862fa0-dafa-4d28572533a"
data['objects'].append({'type':'dashboard', 'id':dashboard_id})


json_string = json.dumps(data, indent=4)
print(json_string)
Output:
{ "objects": [ { "type": "dashboard", "id": "66862fa0-dafa-4d28572533a" } ], "includeReferencesDeep": true }
There could be more objects, other objects types, etc. You need to learn to manipulate json.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you Buran, That is what I needed. yes I never thought that I am setting up json. still stuck in old way of doing (bash scripting)
Reply
#6
If you do stuff in shell scripts, do you know about jq? It's a really handy command line tool for doing stuff with JSON.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing String Variable with a new String Name kevv11 2 773 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Need help on how to include single quotes on data of variable string hani_hms 5 2,012 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 2,794 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 958 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,278 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,780 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,295 Jul-11-2022, 08:52 AM
Last Post: japo85
  Can you print a string variable to printer hammer 2 1,941 Apr-30-2022, 11:48 PM
Last Post: hammer
Question How to convert string to variable? chatguy 5 2,381 Apr-12-2022, 08:31 PM
Last Post: buran
  I want to search a variable for a string D90 lostbit 3 2,618 Mar-31-2021, 07:14 PM
Last Post: lostbit

Forum Jump:

User Panel Messages

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