Issue
The code to make a link for a menu of content is:
<a href="#Section-1">Section 1</a>
In the first part of the tag, the spaces of the title have to be replaced by dashes '-'. The second part just the normal text with spaces between words.
I have tried using:
string.replace(" ", "-")
to replace the spaces with dashes, but when I execute this on the cell, the string comes with ''.
Any ideas? Thanks in advance.
Solution
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 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 just to copy this and paste it on another cell, 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)
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)
Answered By - Jorge C
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.