0

How to format Python code as Python snippets on stack overflow since we can only do it as JavaScript or HTML or CSS snippets?

4
  • 3
    This is probably a question for Meta Stack Overflow Commented Feb 3, 2022 at 20:34
  • 1
    I would imagine that JS, HTML, and CSS are supported as actual snippets because SO is used on a browser, and those are browser-specific languages. There is no expectation that someone would be able to run Python in a browser so the code cannot be executed in a snippet and instead, you're expected to simply format Python code as "code". Commented Feb 3, 2022 at 20:35
  • 1
    Can you clarify what you're asking? Are you asking about properly formatting (highlighting, etc.) posted Python code or about making it executable? "format Python code" and "Python snippets" are two different things, as snippets are about making code executable. Commented Feb 3, 2022 at 20:42
  • @Python_Guy As others have correctly pointed out, only HTML/JS/CSS are supported. However, using pyodide you can run python in the browser and call it from JS. Using this, my project PyPrez allows you to run Python snippets in your Stack Overflow questions and answers Commented Oct 20, 2022 at 1:32

1 Answer 1

4

If you mean executable snippets, you can only do this with html/css/js:

p {
    color: red;
}
<p>You can't do this with Python... sadly</p>

If you want to simply format a snippet, you can do this:

# This is Python code

printing this:

```python
# This is Python code
```

to do this you have to send key ctrl+k on a new line.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.