Reputation: 15
I want to make the input such that the user will type a separate number on each new line. I managed to put together this code
def str2arr(str):
arr = []
for line in str:
arr.append(str.replace('\n', ''))
return arr
import sys
a = sys.stdin.readlines()
print(a)
It is working nearly as I would like to, but the output looks like
['6543\n', '6543\n', '7654\n']
Is there a clever way to remove the \n? And also, will I even get an usable integers using this method?
Thank you guys in advance.
Upvotes: 1
Views: 449
Reputation: 43169
You could use
a = ['6543\n', '6543\n', '7654\n']
integers = list(map(int, a))
print(integers)
# [6543, 6543, 7654]
Upvotes: 3
Reputation: 14516
Have a look at string.strip. That should provide the expected result.
You'll then have to call int()
on each of the entries in the list.
Example:
>>> t = ['6543\n', '6543\n', '7654\n']
>>> [int(x.strip()) for x in t]
[6543, 6543, 7654]
Upvotes: 1