Modify a string's value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s0suk3@gmail.com

    Modify a string's value

    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
  • Ben Finney

    #2
    Re: Modify a string's value

    s0suk3@gmail.co m 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

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: Modify a string's value

      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

      Comment

      • Terry Reedy

        #4
        Re: Modify a string's value



        s0suk3@gmail.co m 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.

        Comment

        • MRAB

          #5
          Re: Modify a string's value

          On Jul 15, 3:06 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
          wrote:
          s0s...@gmail.co m 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.

          Comment

          • Larry Bates

            #6
            Re: Modify a string's value

            s0suk3@gmail.co m 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

            Comment

            • s0suk3@gmail.com

              #7
              Re: Modify a string's value

              On Jul 15, 6:46 pm, Larry Bates <larry.ba...@we bsafe.com`wrote :
              s0s...@gmail.co m 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.writ e(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.readl ine() # which doesn't make sense

              line = ""
              cin >line

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

              Comment

              • Ben Finney

                #8
                Re: Modify a string's value

                s0suk3@gmail.co m 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

                Comment

                • s0suk3@gmail.com

                  #9
                  Re: Modify a string's value

                  On Jul 15, 11:55 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
                  wrote:
                  s0s...@gmail.co m 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 StreamLineBuffe r:
                  def __init__(self):
                  self.buf = ""

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

                  def __str__(self):
                  return self.buf

                  cin = sys.stdin
                  buf = StreamLineBuffe r()

                  cin >buf
                  line = str(buf)

                  Works like a charm :)

                  Comment

                  Working...