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.
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]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With