-2

I have a code something similar to bellow.

name = 'Dave'
message = f'<name> is a really great guy!'
message = message.replace('<name>', '{name}')
print(message)

the variables are a little more complicated than this, and a user (who may not be programming literate) will have entered into a variable via input(). I'm wanting to convert to {name} so fstring can handle the variable.

The expected output would be "Dave is a really great guy!". instead, its outputting "{name} is a really great guy!". Is there a way I can handle an issue like this?

  • `message.replace('', name)`…!? – deceze Jan 27 '23 at 10:12
  • 2
    Why not have `message = f"{name} is a really great guy!"` and change the variable `name` into something else? – Lexpj Jan 27 '23 at 10:13
  • This is XY problem. Fix your f-string literal. – buran Jan 27 '23 at 10:15
  • whats inside the {} or <> are not relevant. a non-programming literate person will have entered the message string, and I'm wanting to change their variable marked in <> to be a functional fstring using the more complicated variable they will not have used – AppropriatePanic Jan 27 '23 at 10:17
  • Try: name = 'Dave' message = f'{name} is a really great guy!' print(message) – JLMelandri8 Jan 27 '23 at 10:20
  • So basically you ask how/is it possible to take user input with placeholder, that will be processed as being f-string? That's not clear from your current question – buran Jan 27 '23 at 10:21
  • Does this answer your question? [How can I use f-string with a variable, not with a string literal?](https://stackoverflow.com/questions/54351740/how-can-i-use-f-string-with-a-variable-not-with-a-string-literal) – buran Jan 27 '23 at 10:22
  • that is correct. I am trying to replace the placeholder with an actual variable. the variables are more complicated than just {name}, I was simplifying it, apparently too simple – AppropriatePanic Jan 27 '23 at 10:22

1 Answers1

1

You seem to be confused about f-strings. An f-string interpolates variables right there in that literal. Consider f-strings as syntactic sugar over the + operator:

message = f'{name} is a really great guy!'
message = name + ' is a really great guy!'

These two lines are equivalent for the purposes of this example. It takes the value of the name variable and bakes it into the string, then assigns the result to message. The result is a regular plain string. There's nothing "f-stringy" about message anymore, it's just the string 'Dave is a really great guy!'.

If you put some curly braces into the string afterwards, it will not be retroactively evaluated as an f-string replacement.

message = '<name> is a really great guy!'
message = message.replace('<name>', '{name}')

This is equivalent to what you're doing. Of course it will only replace "<name>" with "{name}". Literally like that.

Since you're doing replacement anyway, there's no point in using f-strings here. Just replace the placeholder <name> with the variable value:

message = '<name> is a really great guy!'
message = message.replace('<name>', name)
deceze
  • 510,633
  • 85
  • 743
  • 889
  • ah I see, since its not in ' ', it becomes the variable. that was much simpler than I apparently was making it out to be – AppropriatePanic Jan 27 '23 at 10:26
  • Yes. You _could_ do `.replace(..., f'{name}')`, but that's just a complicated way of writing `name`. But `'{name}'` just literally means "{name}". – deceze Jan 27 '23 at 10:27