obami
obami

Reputation: 59

Use \n in Python as a normal string

I am making a string calculator using numbers. For example: 154 + 246 = 154246

So the user will enter an input of a group of numbers, to separate the numbers using \n. As you know, \n is used to make a new line, but I need to use at as any normal string. I need to separate the numbers using \n into a list.

Code:

num_list = []
# this function will add a number to the list
def ask_num():
    # ask for a number (we will make it a string so we can add comma and /n)
    num = input("Enter numbers: ")

    
# run the function for asking numbers
ask_num()

Upvotes: 3

Views: 1948

Answers (3)

obami
obami

Reputation: 59

Successful solution:

def ask_num():
    num_list = []
    # ask for a number (we will make it a string so we can add comma and /n)
    num = input("Enter numbers: ")
    # put the num in the list
    num_list.append(num)
    # seperate the numbers
    num_list = num.split(r"\n")
    # print the list
    print(num_list)

Upvotes: 1

Don CorliJoni
Don CorliJoni

Reputation: 248

You can use

r"\n"

or

"\\n"

Upvotes: 5

FLAK-ZOSO
FLAK-ZOSO

Reputation: 4088

You can escape the \n using an other \.

>>> print("\\n")
\n
>>> print("\n")


>>> print("/n")
/n

In your case, you asked for /n, which is not used for a newline, so you can normally use it in a string.

Upvotes: 5

Related Questions