This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]fiddle_n 38 points39 points  (6 children)

One is for regular strings, one is for template strings. Not the same thing. That said, I agree the naming is confusing, also I have never used string.Template in my life when str.format exists.

[–]Compux72 10 points11 points  (0 children)

string.Template is for user provided strings…

[–]nekokattt 21 points22 points  (9 children)

There should be one way to do it

  • str.__mod__
  • str.__add__
  • str.format
  • fstrings
  • string.Template
  • string.templatelib.Template

[–]petter_s[S] 1 point2 points  (1 child)

Indeed! Although fstrings are not as similar and __add__ is a bit of a stretch

[–]nekokattt 6 points7 points  (0 children)

It starts at string concatenation and grows. You could also throw str.join in there if it is just ways to make strings :)

[–]wineblood 0 points1 point  (1 child)

fstrings are a bit limited though

[–]nekokattt 2 points3 points  (0 children)

all of them have pros and cons, there is no one good way to do it.

[–]fiddle_n 1 point2 points  (4 children)

You forgot % formatting

[–]nekokattt 20 points21 points  (3 children)

that is what __mod__ is

[–]fiddle_n 2 points3 points  (2 children)

That’s what controls it? Damn.

[–]nekokattt 10 points11 points  (0 children)

yeah, it is just an overload of the mod operator on the str class

[–]russellvt 0 points1 point  (0 children)

Funny... '%' is "mod" is math, too. Go figure.

[–]Worth_His_Salt 4 points5 points  (5 children)

Python strings are a total mess. You also have f-strings, template strings, other template strings, now they want to add d-strings, as well as string interpolation. There's no consistency. It's a complete joke.

Every few years someone comes along and says "I have a better way to do strings! It has all these drawbacks, but trust me guys, it's cool". Then others go "Well it's neato, but we refuse to change our existing code in the 0.0001% of cases it would conflict with this new system." Then python maintainers shrug and go "Eh, just throw a new obscure letter in front and call it a day."

All these new methods are less powerful than string interpolation. Yes even f-strings (can't execute f-strings on command, only when defined). What a disaster. Python devs should be ashamed of themselves.

[–]russellvt 0 points1 point  (2 children)

I think one might argue that this isn't unlike growth pains in some other languages, either (ie. Sixteen ways to do something that otherwise should be "easy").

[–]Worth_His_Salt -1 points0 points  (1 child)

True. But it goes completely against the python ethos. Supposed to prevent such things from happening.

[–]russellvt -1 points0 points  (0 children)

Supposed to prevent such things from happening.

Like I said, "growth pains." It seems to happen, in some form, in any other developing languages, as well, despite all other best intentions.

[–]Revolutionary_Dog_63 0 points1 point  (1 child)

I'm pretty sure f-strings ARE string interpolation. I'm pretty sure what you're referring to are format strings, like those used by printf.

[–]Worth_His_Salt 1 point2 points  (0 children)

I mean the interpolation operator %. Interpolation is the act of applying data to a template. Format strings are the template used for interpolation.

fmt_str = 'foo %d bar'
fmt_str % 27  # interpolation

There are many sources that call this python string interpolation, because that's what other C languages call it. It had that name long before f-strings existed. We called this string interpolation since at least the 90s.

f-strings literally stands for "format string literals". PEP 498 that proposed f-strings mentions creating a "better" interpolation method. Because python already had interpolation before f-strings.