-1

I wonder how to write \n, not using \n. What is the 'raw' way to write a new line? Instead of

print("Hello\nWorld")

Output

Hello
World

I want

print("HelloSOMEENCODINGWorld)

Output

Hello
World

Is there a way to use ASCII, Hex, ... within the string?

four-eyes
  • 10,740
  • 29
  • 111
  • 220
  • 3
    What’s your end goal? You have an [XY problem](https://meta.stackexchange.com/q/66377/1968). – Konrad Rudolph Jun 24 '21 at 09:21
  • 1
    @Stophface that edit dose not make the question any more clear. ASCII is an encoding, Hex is a numeral system. Why can't you use `\n`s? – Chillie Jun 24 '21 at 09:23

4 Answers4

1

You can use multi-line strings.

print("""Hello
World""")

But \n is better

Some Guy
  • 13
  • 2
0

One of the options is print('Hello{}World'.format(chr(10))).

Chillie
  • 1,356
  • 13
  • 16
0

One way of doing this might be through os.linesep

import os

print('This is a line with a line break\nin the middle')
print(f'This is a line with a line break {os.linesep}in the middle')

But as stated here:

Note: when writing to files using the Python API, do not use the os.linesep. Just use \n; Python automatically translates that to the proper newline character for your platform.

PiratePie
  • 214
  • 2
  • 7
0

You can use bash ANSI Escape Sequences:

print('Line1 \033[1B\033[50000DLine2',)
# \033[1B Gets cursor to the line below
# \033[50000D Gets the cursor 50000 spaces to the left , 50000 is just a random big number 

You can refer this for more info on Bash ANSI Escape Sequences

Atharva Gundawar
  • 475
  • 3
  • 10
  • 2
    So if I want to write 50001 characters to one line and then add a line break, this will not work properly. Also, when writing this to a file and opening the file in a text editor, it will give unexpected results. Really, why not just `\n` (more a question to @Stophface, not to @Atharva). – mkrieger1 Jun 24 '21 at 09:44
  • 1
    So … you’re now using three escape sequences (two Python-level escape sequences, and one terminal-level ANSI escape sequence) instead of one, and the result only works in highly specific circumstances. … I fail to see how this is an improvement. – Konrad Rudolph Jun 24 '21 at 10:03