I have a function here which should change every letter in the string, apart from the first letter of each word, to an underscore. However, it does not seem to work.
def nameManipulate(title):
positions = []
for letter in range(len(title)):
if title[letter] == " ":
positions.append(letter+1)
positions.insert(0, 0)
print(positions) # Positions of first word of each letter in the string
for letter in range(len(title)):
if letter not in positions: # If the letter is not in the list
new_title = title.replace(str(title[letter]), "_") # Replace the letter with an underscore
return new_title
displayTitle = str(nameManipulate(title))
(the title variable has already been declared and works fine)
The code however doesn't seem to work. It creates an array of positions of all the letters which are at the beginning of the word and changes all those not in that list to an underscore, or should, in theory.
However, when I run the code, this is the output.
(The title in this case was "Jonny B Good")
[0, 6, 8]
Jonny B Goo_
Any help would be greatly appreciated, thank you.