13

I have a string, example:

s = "this is a string, a"

Where a ',' (comma) will always be the 3rd to the last character, aka s[-3].

I am thinking of ways to remove the ',' but can only think of converting the string into a list, deleting it, and converting it back to a string. This however seems a bit too much for simple task.

How can I accomplish this in a simpler way?

martineau
  • 119,623
  • 25
  • 170
  • 301
sqram
  • 7,069
  • 8
  • 48
  • 66
  • 4
    I take it string.replace is out of the question? Do you need to preserve other commas in the string? – John McCollum Jun 18 '09 at 05:49
  • I do. Let me go look into string.replace – sqram Jun 18 '09 at 06:26
  • replace would be useful, if you need to remove many occurrence of a substr, in this case it will be slower too – Anurag Uniyal Jun 18 '09 at 06:30
  • yea, string replace would be ideal if i could start in reverse. I could do x = string.replace(s[::-1], ',', ' ', 1) but that doesn't look to pretty, and whenever i needed to use x, i would have to use as x[::-1] for it to be in its original form =[. Thanks though. – sqram Jun 18 '09 at 06:31

6 Answers6

32

Normally, you would just do:

s = s[:-3] + s[-2:]

The s[:-3] gives you a string up to, but not including, the comma you want removed ("this is a string") and the s[-2:] gives you another string starting one character beyond that comma (" a").

Then, joining the two strings together gives you what you were after ("this is a string a").

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • Thank you =]. Still open to other suggestions if anyone wants to enlighten – sqram Jun 18 '09 at 06:31
  • No, I'm just in it for sharing the love around. But seriously, no, SO doesn't work that way. When I find an answer roughly the same as what I was going to say, I upvote it and sometimes expand. All the rep goes to you. Anyhow, I don't need it, I've come to the realization I will never catch Jon Skeet and even JaredPar is pulling away now that I have to spend more time doing real work :-) – paxdiablo Jun 19 '09 at 02:10
8

A couple of variants, using the "delete the last comma" rather than "delete third last character" are:

s[::-1].replace(",","",1)[::-1]

or

''.join(s.rsplit(",", 1))

But these are pretty ugly. Slightly better is:

a, _, b = s.rpartition(",")
s = a + b

This may be the best approach if you don't know the comma's position (except for last comma in string) and effectively need a "replace from right". However Anurag's answer is more pythonic for the "delete third last character".

Community
  • 1
  • 1
Brian
  • 116,865
  • 28
  • 107
  • 112
3

Python strings are immutable. This means that you must create at least 1 new string in order to remove the comma, as opposed to editing the string in place in a language like C.

Unknown
  • 45,913
  • 27
  • 138
  • 182
1

For deleting every ',' character in the text, you can try

s = s.split(',')
>> ["this is a string", " a"]
s = "".join(s)
>> "this is a string a"

Or in one line:

s0 = "".join(s.split(','))
lmount
  • 1,282
  • 11
  • 9
0

The best simple way is : You can use replace function as :-

>>> s = 'this is a string, a'
>>> s = s.replace(',','')
>>> s
'this is a string a'

Here, replace() function search the character ',' and replace it by '' i.e. empty character

Note that , the replace() function defaults all ',' but if you want only replace some ',' in some case you can use : s.replace(',' , '', 1)

susan097
  • 3,500
  • 1
  • 23
  • 30
0

To slice a string of arbitrary length into multiple equal length slices of arbitrary length you could do

def slicer(string, slice_length):
    return [string[i:i + slice_length]
            for i in xrange(0, len(string), slice_length)]

If slice_length does not divide exactly into len(string) then there will be a single slice at the end of the list that holds the remainder.

wolfson109
  • 898
  • 6
  • 10