-4

I have next two functions:

    def encrypt(self, text):
        for i in range(len(text)):
            text = text[:i] + self.letters[self.letters.index(text[i]) + self.key] + text[i+1:]

    def decrypt(self, text):
        for i in range(len(text)):
            text = text[:i] + self.letters[self.letters.index(text[i]) - self.key] + text[i+1:]

I want them to act on strings in-place. What should I do?

1 Answers1

0

In Python, a string is immutable. You cannot overwrite the values of immutable objects. However, you can assign the variable again.

Elidor00
  • 1,271
  • 13
  • 27
  • But it's ineffective. There is something like passing by reference(c++) in python, isn't it? – silly_willy Oct 18 '20 at 18:41
  • @silly_willy [Python doesn't support pass-by-reference](https://stackoverflow.com/q/986006/4518341). – wjandrea Oct 18 '20 at 18:42
  • 1
    Arguments are passed by assignment. If you pass an immutable object to a method, you still can't rebind the outer reference, and you can't even mutate the object. – Elidor00 Oct 18 '20 at 18:42