sretkaya
sretkaya

Reputation: 23

How can I put the input inside a string which already exist?

I have a string called data, you can see below. I just want to take an input from user and put the input inside ability_id field in data.

def main():
    ability_id = input("Enter an ability id) # example:abc-123-abc-123
    data = '{"paw":"1234","ability_id": f"{ability_id}" ,"obfuscator":"plain-text"}'
    print(data)

Upvotes: 2

Views: 99

Answers (1)

Christian
Christian

Reputation: 1571

While I don't think, saving the string like this has a lot of sense, I will still give you an answer to your question. If you want to do it like this you can, as you already nearly did, use the f-string. But you can't just simply start an f-string inside a normal string - this is not, how this works. Instead, you have to make the whole thing an f-string and then indicate inside this string, where you want to place the variable. Something like this:

data = f'{{"paw":"1234","ability_id": "{ability_id}" ,"obfuscator":"plain-text"}}'

Attention: Watch out, that you use the double curly-brackets at the beginning and ending of the string, otherwise it will not work

Upvotes: 2

Related Questions