0

According to this webarticle: https://www.programiz.com/python-programming/string-interpolation

One can interpolate a variable by using the f function in combination with the mustache signs {}. Second one can use the %-formatter, third one can use the .format() function on an existing string in combination with again the mustache curls {} and lastly there also exists a Template class that allows you insert variables in a string like so:

name = 'world'
program ='python'
new = Template('Hello $name! This is $program.')
print(new.substitute(name= name,program=program))

Why so many different methods to do basically the same thing? Is it because most of those methods came with older python versions and are now considered obsolete?

Thank you

toti08
  • 2,448
  • 5
  • 24
  • 36
FAM_Maurice
  • 363
  • 3
  • 10
  • 2
    They each have slightly different use-cases, and were introduced at different times within python's lifetime. They exist to make your life easier. I'd rather use f-strings than `str.format`, but that's not always possible since `str.format` can do padding and all sorts. – byxor Dec 17 '19 at 11:40
  • what about the other ones like Template? – FAM_Maurice Dec 17 '19 at 12:13
  • and shouldent you use `ljust()` for padding strings? – FAM_Maurice Dec 17 '19 at 12:14

0 Answers0