I have Python code that pulls info from a sqlite database and then write to a html file. I’m trying to write as preformatted text and the code below places the text in columns like I am try to accomplish, but (obviously) the link is the entire length of the string, including the whitespace to the right from the .ljust.
How can I get it to only link the text of item.title instead of the entire string, plus whitespace?
content += '<a href="/%s/">%s</a> %s' % (item.slug, str(item.title).ljust(25), item.date.ljust(10)
If you must do it on one line, the following should work for you:
However the following should be a little easier to read
Notice I’ve added one extra space to your formatting string to make up for the one lost to the string split operation.
EDIT: The above code fails when
item.titleis greater than 25 characters. A revised version is below.This version adds a space to the end of the justified title, so the split operation is guaranteed to return a tuple of length 2.