Question: How do I transform a classic string to an f-string?

```
variable = 42
user_input = "The answer is {variable}"
print(user_input)

```

Output: `The answer is {variable}`

```
f_user_input = # Here the operation to go from a string to an f-string
print(f_user_input)

```

Desired output: `The answer is 42`

Answer: 
```py
variable = 42
user_input = "The answer is {variable}"
# in order to get The answer is 42, we can follow this method
print (user_input.format(variable=variable))

```

(or)

```py
user_input_formatted = user_input.format(variable=variable)
print (user_input_formatted)

```

Good link <https://cito.github.io/blog/f-strings/>
Just to add one more similar way how to do the same.
But **str.format()** option is **much preferable** to use.

```
variable = 42
user_input = "The answer is {variable}"
print(eval(f"f'{user_input}'"))

```

A safer way to achieve the same as Martijn Pieters mentioned above:

```
def dynamic_string(my_str, **kwargs):
    return my_str.format(**kwargs)

variable = 42
user_input = "The answer is {variable}"
print('1: ', dynamic_string(my_str=user_input, variable=variable))
print('2: ', dynamic_string(user_input, variable=42))

```

```
1:  The answer is 42
2:  The answer is 42

```