Percent Sign Does Not Work

I am working with an HTML application with Python. I usually use the % sign to indicate that I am using a Python element and have never had a problem with this before.

Now I use some tables, which I try to control with their sizes, until the percentage using the % sign. So now, Python does not display Python elements.

Here is the code I will explain myself:

 <table width="90%"> <tr> <td width="60%">HELLO</td> <td width="40%">GOOD BYE</td> </tr> </table> <input type="button" value="BUTTON" onclick="function(%s)" /> ''' % variable 

The error I am experiencing says

unsupported character '' '(0x22) with index 19 "

referring to line %s in onclick=function(%s)

Does anyone know if the % sign affects tables in Python or something like that?

+4
source share
2 answers

You need to avoid "%" as "%%" in python lines. The error message you get probably applies to other percentages. If you put only one percent sign in a string, python thinks that it will be followed by a format character and will try to make a change of variables there.

In your case, you should have:

 ''' .... <table width="90%%"> <tr> <td width="60%%">HELLO</td> <td width="40%%">GOOD BYE</td> </tr> </table> <input type="button" value="BUTTON" onclick="function(%s)" /> ''' % variable 
+7
source

In new Python (e.g. 2.6 or later) you can use the .format() method. You will not need to avoid your percentage signs:

 s = '''<table width="90%"> <tr> <td width="60%">HELLO</td> <td width="40%">GOOD BYE</td> </tr> </table> <input type="button" value="BUTTON" onclick="function({funcarg})" /> ''' s.format(**{'funcarg': 'return'}) 
+3
source

Source: https://habr.com/ru/post/1392525/


All Articles