Python Forum

Full Version: Mutable Strings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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)
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.
(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.