By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,810 Members | 783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,810 IT Pros & Developers. It's quick & easy.

Modify a string's value

P: n/a
Hi everyone,

I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?

Thanks,
Sebastian
Jul 15 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
s0****@gmail.com writes:
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
If there were, it would not be immutable. The 'str' type has only
immutable values.

You could implement your own string type, and have it allow mutable
values. You'd have to take care of creating those values explicitly,
though.

--
\ “Pinky, are you pondering what I'm pondering?” “I think so, |
`\ Brain, but shouldn't the bat boy be wearing a cape?” —_Pinky |
_o__) and The Brain_ |
Ben Finney
Jul 15 '08 #2

P: n/a
Sebastian:
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
No, but you can use other kind of things:
>>s = "hello"
sl = list(s)
sl[1] = "a"
sl
['h', 'a', 'l', 'l', 'o']
>>"".join(sl)
'hallo'
>>from array import array
sa = array("c", s)
sa
array('c', 'hello')
>>sa[1] = "a"
sa
array('c', 'hallo')
>>sa.tostring()
'hallo'

Bye,
bearophile
Jul 15 '08 #3

P: n/a


s0****@gmail.com wrote:
Hi everyone,

I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
In 3.0, ascii chars and encoded unicode chars in general can be stored
in a mutable bytearray.

Jul 15 '08 #4

P: n/a
On Jul 15, 3:06*pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
s0s...@gmail.com writes:
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?

If there were, it would not be immutable. The 'str' type has only
immutable values.

You could implement your own string type, and have it allow mutable
values. You'd have to take care of creating those values explicitly,
though.
You could write a C extension which modifies strings, but that would
be a Bad Idea.
Jul 15 '08 #5

P: n/a
s0****@gmail.com wrote:
Hi everyone,

I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?

Thanks,
Sebastian
Why would you care? Just create a new string (with the changed contents) and
let garbage collection take care of the old one when all the references to it
have gone away. Since these types of questions seem to appear almost every day
on this list, this Python stuff is so much different than old languages people
have hard time making the conceptual "jump". You can basically quite worrying
about how/where things are stored, they just are.

-Larry
Jul 15 '08 #6

P: n/a
On Jul 15, 6:46 pm, Larry Bates <larry.ba...@websafe.com`wrote:
s0s...@gmail.com wrote:
Hi everyone,
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
Thanks,
Sebastian

Why would you care? Just create a new string (with the changed contents) and
let garbage collection take care of the old one when all the references to it
have gone away. Since these types of questions seem to appear almost every day
on this list, this Python stuff is so much different than old languages people
have hard time making the conceptual "jump". You can basically quite worrying
about how/where things are stored, they just are.
Thanks for the reply. It's not that I'm having a hard time learning
Python. I've been programming it for some time. I just came across
this unusual situation where I'd like to modify a string passed to a
function, which seems impossible since Python passes arguments by
value. (Whereas in C, you'd customarily pass a pointer to the first
character in the string.)

I was playing around trying to simulate C++-like stream operations:

import sys
from os import linesep as endl

class PythonCout:
def __lshift__(self, obj):
sys.stdout.write(str(obj))
return self

def __repr__(self):
return "<cout>"

cout = PythonCout()
cout << "hello" << endl

But then trying to simulate cin:

class PythonCin:
def __rshift__(self, string):
string = sys.stdin.readline() # which doesn't make sense

line = ""
cin >line

And there goes the need to modify a string. :)

Jul 16 '08 #7

P: n/a
s0****@gmail.com writes:
I just came across this unusual situation where I'd like to modify a
string passed to a function
Again: Why? The normal way to do this is to create a new string and
return that.
which seems impossible since Python passes arguments by value.
No, Python passes arguments by reference
<URL:http://effbot.org/zone/python-objects.htm>.

--
\ “For of those to whom much is given, much is required.” —John |
`\ F. Kennedy |
_o__) |
Ben Finney
Jul 16 '08 #8

P: n/a
On Jul 15, 11:55 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
s0s...@gmail.com writes:
I just came across this unusual situation where I'd like to modify a
string passed to a function

Again: Why? The normal way to do this is to create a new string and
return that.
<snip>

Yes, usually, but that won't work in this case (look at my previous
post around the 'cin >line' line). This is what I came up with:

class StreamLineBuffer:
def __init__(self):
self.buf = ""

def __rrshift__(self, stream):
self.buf = stream.readline()

def __str__(self):
return self.buf

cin = sys.stdin
buf = StreamLineBuffer()

cin >buf
line = str(buf)

Works like a charm :)

Jul 16 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.