0

Sorry for the confusing title, I haven't got the idea how to phrase the question better. Consider this text file:

sampleText.txt

Hi, my name is + Name

when I load the text using msg = open(r'sampleText.txt', 'r') and set a Name = 'John' variables, the Name inside sampleText.txt is ignored and the file was printed as-is.

The desired behavior is

Name = 'John'
msg = 'Hi, my name is ' + Name
print(msg)

which will print Hi, my name is John, I want to be able to load the msg = from a text file.

Sorry for the confusing question, thank you.

  • `msg.replace('+ Name', Name)`…?! – deceze Aug 13 '20 at 08:05
  • 1
    I'd suggest using another syntax, but basically what you want ot do is called "templating", using a "template text" that has some placeholders (your "variables") that you want to fill. I can see several problems using the `+` syntax, that could be avoidable with another syntax. It may look a bit complex at first, but here is the builtin templating system from Python: https://docs.python.org/3/library/string.html#template-strings – Pac0 Aug 13 '20 at 08:05
  • Template seems to be what I'm looking for. Thank you! – ConfusedButterfly Aug 13 '20 at 08:13

0 Answers0