Idea: accept length 0, as in field.center("'",0) to add one quote before and one quote after field

When you gave to add quotes to columns in Pandas (and in normal python), you end up with concatenations or joins a lot. Like
quotedfield = "".join(["'",field,"'"])
or
qfield = "'" + df.field + "'"

If you know in advance that field is, say, exactly 8 bytes long, you could also use field.center(“'”,10), or column.str.pad(“'”,10), which even works in Pandas
qfield =df.field.str.center("'",10)

But if you don’t know the length and don’t want to calculate it explicitly, you’re stuck. Unless the cener/pad method accepted special length value 0 to imply add 1 occurrence of delimited before and after field. So

>>> "Doe, John".center("--",0)
'--Doe, John--'

Does this make sense, would it help code writers, or are there alternatives I had not though about? Note, my current pain point is Pandas, so an extension to center or pad seems to have the most bang for the buck.

The string methods in pandas are not quite just a copy of the standard-library str methods, they wrap those methods in the context of their library to feel similar. So I think this is actually two different changes, one of which is more of a feature request for pandas.

On the stdlib side, there are a lot of easy ways to do this already. e.g. use an f-string f"'{value}'" or even value.join("''"). Doing it in the context of a pandas Series is where it gets tricky.

Apparently you can’t pass a Series into df.field.str.center, which is a shame (I’d like to pass element-wise arguments there). But it’s possible to handle strings with arbitrary lengths using apply and a lambda:

 df.field.apply(lambda x: x.center(len(x) + 2, '"'))

Although there’s an underying question here: do you need to add quotes at all? In my experience, adding explicit quotation marks around string data is a recipe for later confusion. Unless the quotation marks are actually part of the data (and not just formatting), I’d leave them out.

The df I’m working with is pretty long and wide, and several columns need quoting on output, I’m building formatted commands and the command requires quotes whenever there are spaces or special characters in the field value. Yeah, I don’t store quotes for fun.

Since there are many rows in the df I wanted to steer clear of .apply with its row by row calling method if there was a hard coded equivalent, plus .str.center(0,"'") reads (and types) easier than the lambda version.

I like the reverse join approach you showed, I might try to %%timeit it:

Slower methods:

511 µs:  r.users.NAME.apply(lambda x: x.join(["'","'"]))
542 µs: r.users.NAME.apply(lambda x: x.center(len(x) + 2, "'"))
590 µs: "'" + r.users.NAME + "'"

Fastest to produce a Series, except the length value is different for each name, and you cannot pass center() a Series, that is why I suggested the re-definition of length value 0.

405 µs:  r.users.NAME.str.center(20,"'")

I need the output in a column in a df, so output should be a Series, not an Array, because this is the fastest:

142 µs: "'" + r.users.NAME.values + "'"