-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?

3
  • Please edit and ask the question. Dont understand what you mean Commented Oct 18, 2020 at 18:38
  • You cannot change stringds in place - string are not mutable in python - you can only create a new string Commented Oct 18, 2020 at 18:40
  • @Ekure What don't you understand? I understand it perfectly well. Maybe you missed the last sentence? Commented Oct 18, 2020 at 18:40

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

But it's ineffective. There is something like passing by reference(c++) in python, isn't it?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.