-2

First of all, I know -= operation doesn't work in str.

But is there a function that works the same?

The reason why I need this is because

def function(self, str_source):
    str_source = str_source[:-1] # removing last character of the string
    str_source += self.other_function() # adding other characters
    return True

In this sort of function, when I do s = s[:-1], the original string does not change.

I know why it does not change, and I know I can just return another modified string, but I am currently working on someone else's code that I can't complete rip up the Project.

So is it possible to remove the substring of a string in a function?

Sociopath
  • 13,068
  • 19
  • 47
  • 75
BerryMan
  • 145
  • 1
  • 15
  • 7
    No. Python strings are immutable. There is no way to change the original string. Any change, whether cutting off the last character or appending some more, produces a new string, unrelated to the original one. – Amadan Sep 05 '18 at 08:59
  • 1
    dupe? https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out – Jean-François Fabre Sep 05 '18 at 09:03
  • Well, in fact, appending more is possible by using += operation. – BerryMan Sep 05 '18 at 09:04
  • @Jean-FrançoisFabre: The answer is; I wouldn't say the question is. – Amadan Sep 05 '18 at 09:04
  • 2
    @BerryMan No, that also creates a new string with the same name. – L3viathan Sep 05 '18 at 09:05
  • @BerryMan: You are wrong. `s += t` is equivalent to `s = s + t`. Proof: `s = "hello"; t = s; s += " world"; s == t` gives `False` – Amadan Sep 05 '18 at 09:06
  • 1
    Can you show an example of how you would like to use `-=` with a string? (e.g, what is supposed to be the result of `s = 'hello'; s -= 'xy'`?) – mkrieger1 Sep 05 '18 at 09:06
  • @Jean-FrançoisFabre I see. Is it possible to just create a new string str_source[:-1] and save it to where original str_source is? – BerryMan Sep 05 '18 at 09:07
  • @BerryMan if you ask me, I'll give you the same answer: no. you cannot change an existing string because it would break the other parts of code referencing this string. strings are immutable and everybody relies on that. – Jean-François Fabre Sep 05 '18 at 09:08
  • That is basically just modifying `str_source` described in other words, no? Nope, not possible. – Amadan Sep 05 '18 at 09:08
  • @mkrieger1 -= is unsupported operand type between strs – BerryMan Sep 05 '18 at 09:08
  • @Jean-FrançoisFabre Thank you for being clear. I guess I have to rip up the whole Project. – BerryMan Sep 05 '18 at 09:08
  • @BerryMan Yes, but it is unclear what this operator is supposed to *do*. – mkrieger1 Sep 05 '18 at 09:12
  • 3
    @BerryMan: People have done many, many things using Python; whatever you want to do, it is likely it's possible, and this is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). It might not be necessary to rip up the project. The answer to "how to stop hot liquid from draining through sieve" might turn out not to be "line the sieve with metal foil", but "don't cook soup in a sieve". :) – Amadan Sep 05 '18 at 09:15
  • @BerryMan: there might be a hack you can do, Would it be possible to pass the name of the string reference (variable name if you like) as a text string? e.g. `s = 'hello'; obj.function("s")` ? – cdarke Sep 05 '18 at 12:52
  • @cdarke When I looked up online, they say it is not possible to get the name of a variable in Python. I think it would be better to just refactor the Project. Thank you for your advice. – BerryMan Sep 06 '18 at 00:18
  • @BerryMan, I think you misunderstand. I wasn't trying to get the name of an object, I was asking if you can change the function call to pass the name as a string. It is then possible, using introspection techniques, to look at the caller's stack frame and obtain the object, which can then be replaced. I've done this many times. – cdarke Sep 06 '18 at 06:33
  • @cdarke hmm.. I think I still don't get what you mean by that. Can you show me any example? – BerryMan Sep 06 '18 at 08:01
  • I'll have to put it into an answer, I'll work on that now. – cdarke Sep 06 '18 at 08:30

2 Answers2

1

There is no way to change the original string in python. If you want to cuts last characters then prodouce a new string.

For more Details

-= is an assignment operator and assignment operators use for some arithmetic or logical operation.

Suhas Bachhav
  • 403
  • 1
  • 7
  • 28
0

Here's a hack:

import sys

def function(str_source):
    # get existing value
    gvars = sys._getframe(1).f_globals
    lvars = sys._getframe(1).f_locals

    if str_source in lvars:
        dir = lvars
    elif sttr_source in gvars:
        dir = gvars
    else:
        # might want to raise an exception here
        print(str_source, "not found")
        return False

    ext_str = str(dir[str_source])

    ext_str = ext_str[:-1] # removing last character of the string
    # Store
    dir[str_source] = ext_str

    return True

s1 = "hello"
function("s1")
s2 = "world"
function("s2")

print(s1)
print(s2)

Gives:

hell
worl
cdarke
  • 42,728
  • 8
  • 80
  • 84