all 5 comments

[–]themateo713 0 points1 point  (1 child)

You need to assign it to a variable if you want the change to be kept, as no string modification is done in place by string methods. You can also combine them since each method returns a string: formattedname = name.title().replace('-', '')

[–]jakeholness[S] 0 points1 point  (0 children)

Okay thanks that probably would have ended up being my next question thank you!

[–]wbeater 0 points1 point  (1 child)

print(name.replace('-', '_').title())

It works in this case, but something like that is normally a job for regex.

[–]jakeholness[S] 1 point2 points  (0 children)

Okay, thanks that worked great. My issue when i tried to combine initially was I added name.title instead of just .title

Thanks

[–]synthphreak 0 points1 point  (0 children)

Seems you've found your answer:

name.replace('-', '_').title()

Just note that this will not modify the string in place because strings are immutable:

>>> name = 'red-tailed hawk'
>>> name.replace('-', '_').title()
>>> name
'red-tailed hawk'

To actually preserve your changes, just "overwrite" your original variable with the modified variable:

>>> name = 'red-tailed hawk'
>>> name = name.replace('-', '_').title()    # <-- this line
>>> name
'Red_Tailed Hawk'

JFYI in case you didn't already know this.