1

How do I insert the value of a string variable within a string which is not going into Python's print method but going into a list of strings?

#!/usr/bin/env python3

some_var="/some/path/to/somewhere"
my_list_of_paths = ['A=/a/b/c', 'B=/b/c/d', 'C=some_var']

print(my_list_of_paths)

print(my_list_of_paths) should print ['A=/a/b/c', 'B=/b/c/d', 'C=/some/path/to/somewhere'] but it prints ['A=/a/b/c', 'B=/b/c/d', 'C=some_var'] which is not the intention here.

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159

1 Answers1

3

This should work:

#!/usr/bin/env python3

some_var="/some/path/to/somewhere"
my_list_of_paths = ['A=/a/b/c', 'B=/b/c/d', 'C=' + some_var]

print(my_list_of_paths)

The following way of insert the string value with f notation also works:

#!/usr/bin/env python3

some_var="/some/path/to/somewhere"
my_list_of_paths = ['A=/a/b/c', 'B=/b/c/d', f'C={some_var}']

print(my_list_of_paths)
Asocia
  • 5,935
  • 2
  • 21
  • 46
absksn
  • 46
  • 1