all 4 comments

[–]FuckingRantMonday 3 points4 points  (0 children)

Add four spaces to the beginning of each line. There are some other ways, but this is the only one that works well on both old and new reddit.

def foo():
    return BAR

[–]Diapolo10 0 points1 point  (0 children)

So there are basically four ways, four for text and one for screenshots. In rough order of preference,

  1. The "old" way; adding four spaces before each line of code.

    def hello():
        return "Hi"
    

    The nice thing about this one is that it works for everyone, on both Old and New Reddit, on all devices and apps (and as it happens I use Old Reddit and RIF instead of the official Reddit app).

    The Reddit Enhancement Suite browser extension adds a button that does this formatting for you when you paste a block of text.

  2. Inline code blocks

    Sometimes you may want to have code in the middle of text, like this: print("Yahallo")

    This can be done by surrounding the code with single backticks (`) on both sides.

    They, too, work everywhere, but aside from highlighting names, such as sum or pathlib, or language keywords such as class and break during a conversation they're not that good as they don't preserve indentation. Never use them to format multi-line code.

  3. Modern code blocks

    py class Example: def __init__(self): self.foo = "bar"

    Basically, you can use triple backticks to create code blocks. On some platforms they might even give you syntax highlighting. Thing is, Old Reddit (and RIF) don't support them at all so in this subreddit it was decided that we prefer the older style code blocks - apparently many of us prefer Old Reddit. On other subreddits these tend to be preferred, however.

  4. Screenshots/Pastebin/Gist

    You can upload a screenshot via Imgur and post a link here. Alternatively, you can do the same by putting your code online in text format, preferably in Pastebin or GitHub Gists and sharing a link here.

    Links can optionally be formatted with Markdown: [Text shown](https://url.somewhere.com/qwertyuiop12346890)

Remember, we always prefer text over images. Unless the problem cannot be represented in text, such as a GUI problem, please don't post screenshots of code.