Reputation: 59
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
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
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