Code:
str="Hello World"
for i in range(len(str)):
str[i]=str[len(str)-i]
print(str)
This corresponds to an error in Python. What would be the right way to implement this?
Code:
str="Hello World"
for i in range(len(str)):
str[i]=str[len(str)-i]
print(str)
This corresponds to an error in Python. What would be the right way to implement this?
str
is not a good variable name because it masks the built-in function str()
.string = "Hello World"
reversed_string = string[::-1]
try to following:
string = "Hello World"
print string[::-1]
print ''.join(reversed(string))
In fact, I think the key problem of you is that you don't understand that string in python is immutable, yet you can read str[i] but this don't mean that you can change str[i],never do this:
one_string = "..."
one_string[i] = ".."
remember one_string[i]
is read only.
You can just use some standard lib funcion to replace part of the string, but some thing like one_string[i] = "..."
is absolutely wrong.