Daniel
Daniel

Reputation: 23

Replace 2 characters in a string in python

I'm wondering where I'm going wrong. The object is to replace the letters e in a user input with an X and a space with a * . This is all done in python and the result should be hxllo*world

i=0
str=input('please enter a phrase    ')
numberOfChar=len(str)
copy=''

while i < numberOfChar:
    if str[i] == 'e':
        copy=str.replace('e','x')
    elif str[i] == ' ':
        copy=str.replace(' ', '*')
    i = i+1
    print(copy)

Thank you

Bit more info:

It's for college and had to use a while loop and a if function. When I realised that the street.replace referred to the variable name and not mistakenly on my part the function name it all became clear. Thank you all

Upvotes: 1

Views: 2743

Answers (5)

0TTT0
0TTT0

Reputation: 1322

Using the replace method is much simpler than how you are attempting to use it.

phrase=input('please enter a phrase    ')

copy = phrase.replace('e', 'x')
copy = copy.replace(' ', '*')
print(copy)

Upvotes: 0

user8651755
user8651755

Reputation:

If I'm understanding your project requirements correctly, you probably want to avoid str.replace() altogether and go with something like this, which is just a more explicit variation of @Ajax1234's answer:

string = input('Enter a string: ')
collector = []

for char in string:
   if char == 'e':
       char = 'x'
   elif char == ' ':
       char = '*'
   collector.append(char)

new_string = ''.join(collector)

Side note: you don't need the loop variable i to iterate over a string. You can just iterate directly over the characters in the string as shown above.

A standard-library option would be to use str.translate():

>>> table = str.maketrans(' e', '*x')
>>> 'hello world'.translate(table)
'hxllo*world'

Upvotes: 5

atru
atru

Reputation: 4744

some_string.replace(x,y) returns a copy of some_string with x replaced by y. Your program is correctly replacing the values but in the second step (whichever comes first, o to x or space to *) - it still does it on the unchanged original string. Here is a more compact solution - there is no need to do this for every letter:

string=raw_input('Enter text ')
copy = (string.replace('e','x')).replace(' ', '*')
print copy

To demonstrate the point, this is your loop that actually works as intended:

copy = string

while i < numberOfChar:
    if string[i] == 'e':
        copy = copy.replace('e','x')
    elif string[i] == ' ':
        copy = copy.replace(' ', '*')
    i = i+1

The program copies the string from the input and then operates on it while overwriting it every time. This is not necessary though, as replace looks/replaces through the whole string in the first call.

Also, as mentioned in the comments you shouldn't use str as your variable name.

Upvotes: 1

beroe
beroe

Reputation: 12316

You don't need the loop or if statements because .replace() works across the whole string...

mystring = input('please enter a phrase    ')
copy = mystring.replace("e","x").replace(" ","*")

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71461

You can use a dictionary and the get method:

convert = {"e":"x", ' ':"*"}
s = "hello world"
final_s = ''.join(convert.get(i, i) for i in s)

Output:

'hxllo*world'

Upvotes: 1

Related Questions