Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mutable Strings
#1
I am trying to move from Perl to Python. Perl is a bit too esoteric for some of the more complex stuff I want to to in the future, and Python code is alot more intuitive with alot less gibberish.
However, there is a sore spot, and it threatens to be a deal killer for any type of text and filesystem operations: The Immutable String.
In Perl my typical method of string replacemtn is:

$foo =~ s/bar/baz/ig;
$foo =~ s/\-/\_/;g
$foo =~ s/^\s+|s+$//g;
These can go on for dozens and even hundreds of lines.
Its really great that Python has a nearly identical regex language, BUT....
Standard Python will not let me alter an existing string with string.replace or anything else I can seem to find. And I am not about to go through the hassle of converting to arrays (lists) and do replacements that way.
SO, the question is where there is a module, decorator or whatever is necessary to mutate strings and on a large scale? And on a simple straight line-by-line manner. Something like the Perl pattern operator or a function that allows mutation?
Reply
#2
You can't modify a string, but you can reassign a replaced string to the original variable. So a transliteration of your above might be...

import re
foo = re.sub(r"bar", r"baz", foo, flags=re.IGNORECASE)
foo = re.sub(r"-", r"_", foo)
foo = re.sub(r"^\s+|s+$", r"", foo)
Reply
#3
Immutability is rarely a problem in practice. Some of us use languages where both the values are immutable and we choose to use immutable references to them (for me, those languages are Kotlin and Clojure). One can still write readable software in those.
Reply
#4
(Aug-24-2020, 05:02 AM)bowlofred Wrote: You can't modify a string, but you can reassign a replaced string to the original variable. So a transliteration of your above might be...

import re
foo = re.sub(r"bar", r"baz", foo, flags=re.IGNORECASE)
foo = re.sub(r"-", r"_", foo)
foo = re.sub(r"^\s+|s+$", r"", foo)

I will inscribe this in granite. IT WORKS!
re.sub does have certain niceties over =~ s///
It seems to autostrip leading/trailing spaces, and the syntax, even with duplicated string name is simple, *readable* and consistent with Perl regex (though not so readable, is a gold standard.

Thank you ;)

(Aug-24-2020, 05:39 AM)ndc85430 Wrote: Immutability is rarely a problem in practice. Some of us use languages where both the values are immutable and we choose to use immutable references to them (for me, those languages are Kotlin and Clojure). One can still write readable software in those.

I can see a point in mutable and immutable arrays (lists vs tuples)

But not with strings. For me the greatest purpose of scripting is manipulating strings.

Sometimes I dont like Mary having a Little Lamb.
Cannot resist turning it into a Big Red Wolf.

Might even be able to script my perl into Python with this format.
I have already been able to fix one of its gotchas.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "'DataFrame' objects are mutable, thus they cannot be hashed" Mark17 1 137 Dec-25-2020, 02:31 AM
Last Post: tinh
  What is the meaning of mutable data type? qliu 3 565 Apr-17-2020, 07:20 PM
Last Post: deanhystad
  copying parts of mutable sequences Skaperen 1 547 Dec-02-2019, 10:34 AM
Last Post: Gribouillis
  Finding multiple strings between the two same strings Slither 1 814 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  A mutable string?? silentknight85 5 1,787 May-31-2019, 10:11 AM
Last Post: silentknight85
  lists, strings, and byte strings Skaperen 2 1,940 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  compacting a mutable sequence Skaperen 6 1,745 Jan-23-2018, 03:54 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020