How do I format code in WordPress?

Formatting code requires a code block: Add Block > Text > Code:

code block

You can find more information regarding formatting code at: https://wordpress.com/support/wordpress-editor/blocks/code-block/