Victor José
Victor José

Reputation: 387

How can I remove these parenthesis in the output?

This is probably a silly question but I don't really know much about Python yet. I'm writing this code that is suppose to take the input for values of X and the input for values of Y, write them in files and print them on the screen (later on I'll plot them with matplotlib). But I'm getting on my outputs two parenthesis that I didn't want them to be there. Here's the code (I'm programming on Ubuntu with Python 3.4):

xcoord = open("x.dat","w")
xinput = input('Enter with the values for x separeted by coma (ex: 10,25,67):')
xcoord.write(str(xinput))
xcoord.close()
ycoord = open("y.dat","w")
yinput = input('Enter with the values for y separeted by coma (ex: 10,25,67):')
ycoord.write(str(yinput))
ycoord.close()

xcoord = open("x.dat","r")
listax = xcoord.read().split(',')
ycoord = open("y.dat","r")
listay = ycoord.read().split(',')
print listax
print listay

The output I'm getting in the files is something like (10, 20, 30) and the output I'm getting on the screen is something like ['(10','20','30)']. How can I get rid of these parenthesis?

Upvotes: 3

Views: 13364

Answers (2)

Raphael Amoedo
Raphael Amoedo

Reputation: 4475

You can remove first and last character on printing:

print listax[1:-1]

Upvotes: 2

famousgarkin
famousgarkin

Reputation: 14126

You can do with str.strip:

>>> '(10, 20, 30)'.strip('()')
'10, 20, 30'

Upvotes: 5

Related Questions