Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

How to include a backtick in a post with formatted code block

Including a backtick ("`") with a triple-backtick ("```") code block messes it up the formatting. No obvious way to include a backtick or escape it so it prints correctly and does not affect the formatted code block.

Base example using ```python {code} ``` works without an included backtick:

$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> answer = (False, "alice")
>>> if answer:
...     print( "True, because 'answer' exists" )
... else:
...     print( "False" )
... 
True, because 'answer' exists

But the code block formatting blows up with HTML escaping if a backtick is within block:

# output from python shell session
$ python3
Python 3.4.0 (default, Jun 19 2015, 14:20:21) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> answer = (False, "alice")
>>> if answer:
...     print("True, because 'answer' exists [include raw backtick `]")
... else:
...     print("False")
... 
True, because 'answer' exists [include raw backtick `]

Trying to escape the backtick with \` does not work. Nor does manually replacing the backtick ` with the codeblock with %#96; does not help.

Colin Marshall
Colin Marshall
32,861 Points

Another issue with backticks on the forum is when you do the single backticks to place code inline in your post. If you have any sort of html tags inside of the inline code they get removed when the post gets displayed.

For example, the inline code below has two div tags surrounding it, but they do not show up in the post. Only the stuff inside the tags gets displayed. I notified support about this some months ago.

<div>text inside of two div tags</div>

1 Answer

You can indent the code block with four spaces, but you lose the syntax highlighting:

# output from python shell session
$ python3
Python 3.4.0 (default, Jun 19 2015, 14:20:21) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> answer = (False, "alice")
>>> if answer:
...     print("True, because 'answer' exists [include raw backtick `]")
... else:
...     print("False")
... 
True, because 'answer' exists [include raw backtick `]

You can also use double backticks to include an inline ` backtick.

Oh and for HTML tags, use the HTML entities, but not within the backticks:

&lt;`div`&gt;`text inside of two div tags`&lt;`/div`&gt;

<div>text inside of two div tags</div>

Or just stick to code blocks.