Question

I'm trying to slice this string to get each value by doing s[0] which return Name1 without the quotes, and doing s[1] would return Surname1 with no quotes and so on. I am unsure on how I can go about doing this, how?

s = ('Name1', 'Surname1', 1, 'god', 1, 0)
Was it helpful?

Solution

It sounds like you're confusing the representation of the data with its value. The quotes help you realize that a string is being represented, but the value of the string is the characters within the quote. If you want to actually see the value without the quote, print it.

print(s[0])

Python lets you define the representation of data in custom classes by modifying its __repr__ method. For more, read up on the repr.

OTHER TIPS

I think you're seeing quotes due to viewing the wrong type of output.

s = ('Name1', 'Surname1', 1, 'god', 1, 0)
print(s[0])

will print Name1 without quotes.

In [18]: s = ('Name1', 'Surname1', 1, 'god', 1, 0)

In [19]: s[0]
Out[19]: 'Name1'

In [20]: print(s[0])
Name1

s[0] is a string and as such is represented with the surrounding quotes. If you simply wanted to see it on screen without quotes, then you could print it, as shown in line 20 of my code.

On the other hand, if you have a variable named Name1 somewhere else in your code, and you are trying to display its value by calling s[0], you might need to store a map of variable names and values in a dictionary (or use locals() or globals())

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top