1

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

3
  • 2
    str is a python type, so you shouldn't use it as a variable name. Commented Oct 20, 2017 at 0:34
  • Could you explain what goes wrong exactly? "Something is wrong" is not a real problem statement. Commented Oct 20, 2017 at 0:41
  • The str comment really helped when I realised that it was a big mistake to use that, thank you Commented Oct 20, 2017 at 0:50

5 Answers 5

5

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'
Sign up to request clarification or add additional context in comments.

1 Comment

table = str.maketrans({'e': 'x', ' ': '*'}) ought to work too.
1

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'

4 Comments

You don't think this is overkill?
There's also probably a translate method or something to do this for you.
@MadPhysicist I believe this is the cleanest, most pythonic method of solving the problem. Plus, if the OP has additional replacement rules later on, far more than the original two, this method will be much more effective.
str has a method to do this for you: stackoverflow.com/a/46841079/2988730. Your answer is perfectly functional, but certainly not the "cleanest and most Pythonic" solution by any means.
1

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(" ","*")

Comments

1

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.

Comments

0

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)

2 Comments

Due to the restrictions on the project we have to use if functions but I was looking at it all wrong and thanks to you I see my mistake
this will only replace the first instance of each character: pretty sure that's just plain wrong: ideone.com/gS271H

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.