1
$\begingroup$

How do I paste properly formatted code in Stack Exchange?

Whenever I try and post code snippets it gets posted as regular text

$\endgroup$
0

2 Answers 2

4
$\begingroup$

I prefer to use triple backticks ``` as they clearly define borders of code, as opposed to the default way of indentation (what if you have empty lines in your code? You can't see if it's supposed to be two separate codes or not). Put both opening triple backticks and closing triple on separate lines, additionally make sure there's an empty line after the closing backticks, as I noticed otherwise formatting can get broken... Finally, you may make it clear which triple is the start of the code, by putting "py" afterwards. In some places it tells the syntax highlighter that it's supposed to use Python rules, but here on BSE it changes nothing. Edit this message to see how I use this below:

print("Hello BSE!")

If you want to put some code inline, just wrap it inside single backticks - it also works in comments.

$\endgroup$
2
$\begingroup$

just select your code, then click on the brackets here

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks. How do I paste code snippets in a comment? $\endgroup$
    – MattL
    Sep 17, 2022 at 14:42
  • $\begingroup$ You can, but they would never be formatted $\endgroup$
    – Chris
    Sep 17, 2022 at 15:03
  • $\begingroup$ If my answer helped you, please click on the checkmark left of my answer. Thanks $\endgroup$
    – Chris
    Sep 17, 2022 at 15:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .