What is wrong with the following Python code for replacing "." with "-"
x = 'BRK.B'
if "." in x
spot = x.find('.')
x(spot)="-"
What is wrong with the following Python code for replacing "." with "-"
x = 'BRK.B'
if "." in x
spot = x.find('.')
x(spot)="-"
You have some typos, that makes your code unworkable.
Even if you fix this, x
is a string, and string is not mutable.
You can just use str.replace
.
x = x.replace('.','-')
You could just use replace
:
>>> 'BRK.B'.replace('.', '-')
'BRK-B'
If you only want to replace the first occurence :
>>> 'BRK.B'.replace('.', '-', 1)
'BRK-B'
If for some reason, you really want to do it yourself :
x = 'BRK.B'
if "." in x: # <- Don't forget : after x
spot = x.find('.')
# You're not allowed to modify x, but you can create a new string
x = x[:spot] + '-' + x[spot+1:]
print(x)
# 'BRK-B'