If you try using the print() function, it will print the string saved in your variable without the quotes:
print(your_string)
However, I think a better way to create a hyperlink for a table of content and avoid having to do-dashes-in-all-the-words, would be to use input for the text, save it to a variable, use the replace() function, and the .format method.
If you are looking to copy this and paste it on the markdown cell to make the TOC, try the following:
import pyperclip as pc
string = input('Enter your string:')
dashed_str = string.replace(" ", "-")
link = ('<a href="#{}">{}</a>'.format(dashed_str, string))
pc.copy(link)
You would have only to run this on a cell, enter the title of the hyperlink, press enter, and the result hyperlink would be on your clipboard to paste it on the markdown cell where you are making the table of content.
I suggest you install and import the pyperclip library to save the string to the clipboard instead of printing it then copying it—fewer clicks to do.
Also, you could use the markdown syntax to make links to sections:
[Section title test](#Section-title-test)
So the code would look like this:
import pyperclip as pc
string = input('Enter menu title:')
dashed_str = string.replace(" ", "-")
link = ('[{}](#{})'.format(string, dashed_str))
pc.copy(link)

If you want to use a function instead:
def link_menu(x):
import pyperclip as pc
dashed_title = x.replace(" ", "-")
link = ('[{}](#{})'.format(x, dashed_title))
return pc.copy(link)
Then you can call your function passing as an argument the string you want for the hyperlink like so:
link_menu('Section 4 this is a test')
This will copy to your clipboard:
[Section 4 this is a test](#Section-4-this-is-a-test)