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)
Answer from Alexander Ejbekov on Stack OverflowYou 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)
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])
python - Replacing instances of a character in a string - Stack Overflow
Python: How can I replace one specific character on a string while leaving the rest of the string as it was?
Python replace character without replace method - Stack Overflow
Str.replace of a set of characters - Ideas - Discussions on Python.org
Strings in python are immutable, so you cannot treat them as a list and assign to indices.
Use .replace() instead:
line = line.replace(';', ':')
If you need to replace only certain semicolons, you'll need to be more specific. You could use slicing to isolate the section of the string to replace in:
line = line[:10].replace(';', ':') + line[10:]
That'll replace all semi-colons in the first 10 characters of the string.
You can do the below, to replace any char with a respective char at a given index, if you wish not to use .replace()
word = 'python'
index = 4
char = 'i'
word = word[:index] + char + word[index + 1:]
print word
o/p: pythin
word1 = input("Word: ") # lets say that the given word is "tower"
word2 = "********************"
word2 = word2.replace(word[3], word[3])
print(word2)
# Now the my code replaces all the "*" characters with the character "e"
# It just prints "eeeeeeeeeeeeeeeeeeee"
# I would like the code only to replace the 4th character with the other strings 4th
# I'd like it to print "***e****************"
This works:
test_string = "Hallo"
# turn the string into a list
test_string = list(test_string)
# change the character you want
test_string[0] = "T"
# convert the list back to a string.
test_string = "".join(test_string)
Strings are immutable, so if you don't want to convert to a mutable type, you'll have to create a new string. Here is an example function that does that:
def replace_(original_string, replace_string, index):
return original_string[:index] + replace_string + original_string[index + len(replace_string):]
print(replace_("Hallo", "T", 0))
print(replace_("Hallo", "Te", 0))
This outputs:
Tallo
Tello
I'd like to note that I prefer the answers that convert to a list; this answer is only provided for a pure-string implementation.