tags:

views:

58

answers:

4

I have a string

string='texttexttextblahblah",".'

and what I want to do is cut of some of the rightmost characters by indexing and assign it to string so that string will be equal to texttexttextblahblah"

I've looked around and found how to print by indexing, but not how to reassign that actual variable to be trimmed.

+3  A: 

Just reassign what you printed to the variable.

>>> string='texttexttextblahblah",".'
>>> string = string[:-3]
>>> string
'texttexttextblahblah"'
>>>

Also, avoid using names of libraries or builtins (string) for variables

Unless you know exactly how many text and blah's you'll have, use .find() as Brent suggested (or .index(x), which is like find, except complains when it doesn't find x).

If you want that trailing ", just add one to the value it kicks out. (or just find the value you actually want to split at, ,)

mystr = mystr[:mystr.find('"') + 1]
Nick T
This is exactly what I was hoping for. Thank you!!!
Brian
+2  A: 

Strings are immutable so you can't really change the string in-place. You'll need to slice out the part you want and then reassign it back over the original variable.

Is something like this what you wanted? (note I left out storing the index in a variable because I'm not sure how you're using this):

>>> s = 'texttexttextblahblah",".'
>>> s.index('"')
20
>>> s = s[:20]
>>> s
'texttexttextblahblah'
Brent Nash
+1  A: 

If you need something that works like a string, but is mutable you can use a bytearray

>>> s=bytearray('texttexttextblahblah",".')
>>> s[20:]=''
>>> print s
texttexttextblahblah

bytearray has all the usual string methods

gnibbler
A: 

I myself prefer to do it without indexing: (My favorite partition was commented as winner in speed and clearness in comments so I updated the original code)

s = 'texttexttextblahblah",".'
s,_,_ = s.partition(',')
print s

Result

texttexttextblahblah"
Tony Veijalainen
He wanted the " in the result (could be a typo), though.
Nick T
`s=s[:s.find('"')]` is more efficient
gnibbler
I did the change myself, as I noticed it, I prefer more pythonic split or partition myself. That is, I prefer "Make it work in clear way, then optimize."
Tony Veijalainen
@Tony, in this case `partition` is more appropriate then, as it conveys that you are splitting something into two parts. `split` conveys the idea that you are splitting by a delimiter in multiple places.
gnibbler
`s = s.split(',',1)[0]` and you don't have to allocate a dummy variable.
Nick T
`split()` is also 50% slower than `partition()` for this example
gnibbler
@Nick, still a lot slower than partition()
gnibbler
`s=s.partition(',')[0]` or `s,_,_=partition(',')` in case anyone reading the comments doesn't know about `str.partition`
gnibbler
@gnibbler: yeah, `partition()` is probably the best tool; was commenting back to OP about dumping the split to 2 variables, though not sure if that was just to illustrate it provides a length-2 iterable.
Nick T
Is split really very slow? I liked idea from this forum that you can split 3 parts by doing: first, second, third, cdr = mytext.split(separator,3) (Ok, I do know Lisp)
Tony Veijalainen