-1

When I do the following:

def encrypt(string):
    str(string).replace('0', 'A')
    return(string)

encrypt(000)

Only 0 comes out as the output.

What I want to do is replace all 0s with an A to test an encryption program I am going to make. The output I want is AAA.

So why doesn't it work when I try to replace 0 with A?

jbihan
  • 3,053
  • 2
  • 24
  • 34
J. Doe
  • 35
  • 1
  • 4
  • 1
    The parameter to encrypt is not quoted? Then you are passing the value `0` or zero, not the string '000' ? – Paul Jan 24 '15 at 02:16

4 Answers4

4

Python strings are immutable. No method or operator therefore is able to mutate the string in-place as you appear to desire.

Rather, they return a result that you must assign to a name (or return, whatever).

So, use:

return str(string).replace('0', 'A')

BTW, immutable strings are such a great idea that Java and many later languages copied them from Python...:-)

Of course, if you use 000 with NO quotes as the argument, the function has and can have no idea how many zeros you typed -- it just receives the number 0! You'd better pass a string (quotes around it), so probably lose the str here, too.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • This doesn't return 'AAA' for input 000, it returns 'A' for input 000... – hft Jan 24 '15 at 02:22
  • @hft, edited to explain -- passing `0`, `000`, `000000000000` or whatever, with no quotes, means the function just receives `0` and can't dream up how many zeros you uselessly typed to indicate that one and only number. Using quotes around the argument at the call site is obviously needed, as your answer mentions (but I thing the bug about not understanding strings are immutable goes even deeper:-). – Alex Martelli Jan 24 '15 at 02:27
  • Thanks for your 'many later languages copied them' point, never really considered it too much until now, but got me thinking (or rather [searching for the relevant SO post](http://stackoverflow.com/questions/8680080/why-are-python-string-immutable-best-practices-of-using-them)) – zehnpaard Jan 24 '15 at 03:07
0

you need to assign the change to the string.

def encrypt(string):
    return str(string).replace('0', 'A')
Tanveer Alam
  • 5,185
  • 4
  • 22
  • 43
user3394040
  • 947
  • 13
  • 22
  • This works, too, but there's no special reason to make another local variable rather than just returning the expression's result as in my answer. "Entities must not be multiplied beyond necessity", as Occam put it:-). – Alex Martelli Jan 24 '15 at 02:16
0

You will not be able to get 'AAA' out if you pass the NUMBER 000. Because the number 000 is the same as 0 and gets converted to the string '0'. If you properly implement your return/replace function as described in the other answers the result for inputting the number 000 with return 'A'.

Probably you want a new function that you pass strings not numbers otherwise you will never be able to differentiate between 000 and 0 (or any other number of zeros 000000000000, etc)

BTW, you called your function variable "string" even though you are passing a number and then converting it to a string with str. This is good foreshadowing. Here's a function that will do what you want with strings, not numbers:

def encrypt(string):
    return string.replace('0', 'A')

>>> encrypt('000')
'AAA'
hft
  • 1,245
  • 10
  • 29
0

There is nothing wrong with your code. You should pass 000 as string if you don't 000 is treated as 0 so you get only one A replacing single 0.

def encrypt(string):
  return str(string).replace('0', 'A')

print encrypt('000')

This gives AAA as output.

Sumit Arora
  • 91
  • 1
  • 5