1

I am creating an HTML output file using vanilla Python 3 and I wish to use the multi-tab layout (http://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_tabs_close) to help organize my presentation.

When trying to print: ' <li><a href="#" class="tablinks active" onclick="openTab(event, 'Faults')"><b>Faults</b></a></li>' I run into a situation where I need to include (') and (") characters in the string. I have tried to only use (") characters inside the string and use (') to define the string, however this breaks the html functionality of the code.

If I use 'partial string {}Faults{} more string'.format("'","'") I see this output:

'  <li><a href="#" class="tablinks active" onclick="openTab(event, \'Faults\')"><b>Faults</b></a></li>)'

Similarly if I attempt to join strings together,

b = '  <li><a href="#" class="tablinks active" onclick="openTab(event, '
c = "'Faults'"
d = ')"><b>Faults</b></a></li>)
('').join([b,c,d])

I get the exact same thing as my first attempt.

'  <li><a href="#" class="tablinks active" onclick="openTab(event, \'Faults\')"><b>Faults</b></a></li>)'

Is there any way to get around this issue while printing to a file? Thank you.

2
  • "I get the exact same thing as my first attempt." Is that what you get when you print it? When you look at the result in the interactive prompt? Or when you write it to a file and open the file with Notepad? Each of these may display differently from the others. Commented Oct 20, 2016 at 16:22
  • 1
    \' is an escape sequence and is correct. 'singled \'quote\' example' = singled 'quote' example Commented Oct 20, 2016 at 16:22

3 Answers 3

1

' <li><a href="#" class="tablinks active" onclick="openTab(event, \'Faults\')"><b>Faults</b></a></li>'

You need to escape single quotes in single quotes. If you write this to a file, it will be how you want it.

Or use ''' <li><a href="#" class="tablinks active" onclick="openTab(event, \'Faults\')"><b>Faults</b></a></li>'''

Notice the triple quotes?

Interesting article about escaping in Python: Learn Python the hard way

Sign up to request clarification or add additional context in comments.

1 Comment

This really helped! Thanks for pointing out the resources I can read to learn more. I appreciate that. I've worked with \n before but didn't think about any other related options.
0

There is no problem with your format approach; what you are seeing is just Python's default representation of the string. Use print '...'.format("'", "'"), and it should look as you expect.

>>> print '  <li><a href="#" class="tablinks active" onclick="openTab(event, {}Faults{})"><b>Faults</b></a></li>'.format("'", "'")
  <li><a href="#" class="tablinks active" onclick="openTab(event, 'Faults')"><b>Faults</b></a></li>

However, you can avoid using format by simply using a multiline string literal ("""...""" or '''...'''), which can accommodate unescaped quotation marks of either flavor.

>>> print '''  <li><a href="#" class="tablinks active" onclick="openTab(event, 'Faults')"><b>Faults</b></a></li>'''
  <li><a href="#" class="tablinks active" onclick="openTab(event, 'Faults')"><b>Faults</b></a></li>

Comments

0

Use the triple ''' or """ characters. This is a multiline string (and docstring) in python.

E.g:

'''
hello world! I can write ' and " and also " and ' "''""'''"""
'''

3 Comments

It's not a comment; Python still has to create the string object defined by the literal.
Yes, it is a document string.
Single-line strings can also be used as doc strings, and multiline strings can be assigned to names (v = """..."""). The two concepts are entirely orthogonal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.