1

This is mostly a logic problem. I am trying to replace a character in a string without using the replace function. I am trying to first change it into a list, change the elements, then turn it back into string. My attept:

def changeCar(ch,ca1,ca2):
    a=list(ch)
    for x in a:
        if x==ca1:
             x==ca2
    return a

However this doesn't work. Besides, I am not sure how to transform it back into a string.

1
  • How doesn't it work? What do you expect and what actually occurs? Commented Dec 13, 2014 at 23:04

5 Answers 5

2

You don't need to define a string as a list in python.

string = 'The quick brown fox jusmps over the lazy dog'
# Define your variables
result = ''
for i in string:
        if i == 'o':
                i = '0'
        result += i
print result

If you MUST however use a list:

string = list('The quick brown fox jusmps over the lazy dog')
result = []
for i in string:
        if i == 'o':
                i = '0'
        result.append(i)
print ''.join(result)
Sign up to request clarification or add additional context in comments.

2 Comments

The list approach is preferable to concatenating strings in a loop (see here).
Thank you! I don't actually have to use lists, I just thought it would be sensible. But yes, leaving it a string is neater. :)
1

You're on the right track. Since Python strings are immutable, you aren't able to change the characters in place, so the approach of spliting it into a list of characters, modifying the elements inside that list, and then re-joining it is correct. Your solution isn't working because x isn't a reference (or pointer) to the elements inside the list, but a copy of them. Thus, x = c2 (and not x == c2, as your example shows) only modifies the copy. You'll have to access the list by index, like so:

for ind in range(len(a)):
    if a[ind] == ca1:
         a[ind] = ca2

return "".join(a)

You can also use a list comprehension for maximum brevity, though it might be a little unreadable:

return "".join([(char if char != ca1 else ca2) for char in ch])

Comments

1

Don't change the string into a list. That's unnecessary. Instead, define an empty string in the function definition and then append to it accordingly and in the end return the value of that variable:

def changeCar(ch,ca1,ca2):
    b = ''
    for x in ch:
        if x!= ca1:
            b+= x
        else:
            b+=ca2
    return b

Now you get:

>>> changeCar("abc","b","c")
'acc'
>>> 

Comments

0

a == b is a Comparison Expression. a = b is an Assignment Statement. To turn a list into a string:

>>> ''.join(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
'abcdefg'
>>> 

Comments

0

For simple manipulations like this you can use list comprehensions. Try it like this:

In [1]: text = "Hello, World!"
In [2]: ''.join([ch if ch != 'o' else 'a' for ch in text])
Out[2]: 'Hella, Warld!'

This list comprehension replaces the character 'o' with 'a'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.