1

I had a situation where I wanted to remove a comma from the end of a string in Python. I thought there must be some idiomatic way of doing it, so I did a quick Google search: remove the last character in a string python. I clicked on the first Stack Overflow question to get the answer.

It is marked as a duplicate of this question about slice notation, and that isn't even a question. No matter how many times you read it, "Explain slice notation" is not a question. Semantics aside, you could argue that removing the final character from a string is a subset of questions related to slice notation, but it is a stretch to mark it as a duplicate. If we take this argument to the extreme, should we then mark all Python questions that involve slice notation as a duplicate of the "Explain slice notation" question?

The truth is that I realized there was more to my original question. My Python construct looked similar to this:

if s.endswith(","):
    s = s[:-1]

After some research, I concluded that it can be Pythonified like so:

s = s.rstrip(",")

It removes all of the commas that appear at the end of the string. It is beautiful and correct.

I would like to add that answer to the post where I fond he answer via Google but because it is marked as a duplicate, I cannot. And I cannot in good conscience place that answer in the other thread because it has absolutely nothing to do with the subject there, slice notation. However, it has everything to do with removing characters at the end of a string in Python.

I do not wish to ask another question on Stack Overflow to present my answer, since it ought to be marked as a duplicate of the Q I found with the answer, right? I tried flagging it once already to no avail, and I see many, many users seem to think that the question really is a duplicate. If I ever go looking for how to remove characters at the end of a string in Python, I want to be able to find the rstrip solution quickly. (Truth be told, with all of the effort I'm putting into getting the answer posted in Stack Overflow, I will probably remember this idiom for a very long time.)

So can we unmark it as a duplicate? If not, what can we do to get my answer posted in a reasonable location?

1
  • The question you linked asks for only the last character to be removed regardless of what it is or how many times it occurs which means that your suggestion of rstrip will not work since it can remove more then 1 character or none at all if it doesn't match what was provided.
    – Joe W
    Feb 26, 2019 at 21:24

1 Answer 1

1

No, "remove last character" and "Remove last/all comas at the end" are very different questions with only some answers that will be common... So re-opening based on the fact that answers don't solve your different problem does not sound right. Even adding "trim particular character" answer to "remove last character" question likely will be considered incomplete/unrelated answer.

Sounds like you either need to find another duplicate or create self-answered question. Trimming string frequently called "trim" so search like https://www.bing.com/search?q=python+trim+comma+end should give you decent results. I have no experience with Python - maybe Python string function to strip the last comma would be better duplicate.

3
  • I understand that the questions may be slightly different, but I sometimes I do not ask quite the right question. When I review the answers on StackOverflow on such a question, I sometimes come across someone in a similar situation, but they have posted the answer that solves their particular problem. I consider it a great feature of StackOverflow. A newcomer may not have the right terminology. (Trim vs strip vs remove) And your attitude indicates to me that I should stop pursuing this any further. But I am curious why you think the two questions I have linked are duplicates.
    – Bobort
    Oct 5, 2017 at 16:28
  • @Bobort I don't know Python so I can't judge if "remove final character" must be duplicate of "slice" - looks at least plausible to me, so one need to provide better reason to re-open than one I understood from your question. As I said removing particular character from the end not necessary will be duplicate as many libraries/languages have special methods for that operation (but not "remove any last character") and hence would have more answers than basic "remove last character" (based on your remark in question Python indeed has better way for known character). Oct 5, 2017 at 17:09
  • You don't know Python. If you were in a situation where you had a string and you wanted take off the last character of the string if it is a comma, how would you go about looking that up? I'm thinking you would do the same thing I did or something similar. You would be given the link to the removing a character at the end at the top of the search page, and not at all the link to slice notation (because Google understands language better than some of our users, apparently). You might use that first answer provided and move on never knowing that there might be something better to it.
    – Bobort
    Oct 5, 2017 at 18:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .