-3

I have a string like this:

Variable = ''

S = """
This is my """+Variable+""" in my string"""

def fun():
    Variable = 'Changed Variable'
    NewS = """
    First sentence. """+S

Desired output:

  First sentence. This is my Changed Variable in my string

What I get now: First sentence. This is my in my string

is I put the variable in the string, and then use that as a string, how do I update the variable in that string later?

user3486773
  • 1,174
  • 3
  • 25
  • 50
  • 3
    You cant. Strings are immuteable. You need to create the full string again. You also use two Variable in different scopes - they do not see each other. – Patrick Artner May 07 '20 at 14:01
  • https://stackoverflow.com/questions/9097994/arent-python-strings-immutable-then-why-does-a-b-work – Patrick Artner May 07 '20 at 14:02

1 Answers1

2

Just use a formatted string or a regular string and add the var to it:

string = f"This is my {Variable} in my string"
Roy
  • 1,612
  • 2
  • 12
  • 38