Converting List and tuples using str() function

In summary, the conversation discusses the difference in character count when converting a tuple or list to a string using the str() function. The speaker realizes that the additional characters come from spaces and delimiters in the converted string.
  • #1
Taylor_1989
402
14
TL;DR Summary
I am slightly confused to why using the str() on a tuple and list produces a length greater than a a defined string when comparing lengths
I am currently working my way through some w3schools python exercise on tuples and lists etc and one question was to write a program to converted a tuple to a string.

Now originally I used the str() function on the tuple and printed the result. I then used the string in a for loop for a further question and realized that the tuples and lists were printing additional terms compared when just defining a string using ' '.

As can be seen from my code below

Python:
t =(1,2,3,4,5)
L =[1,2,3,4,5]

string = '1,2,3,4,5'

t = str(t)
L = str(L)

length = len(t)
length1 = len(L)
length2 = len(string)

print(length)
print(length1)
print(length2)

Output
15
15
9

My question why is this, why is there 15 for the list and the tuple yet 9 for the string? Where is the additional six count coming from in the tuple and list?
 
Technology news on Phys.org
  • #2
Taylor_1989 said:
Where is the additional six count coming from in the tuple and list?

Try printing the actual strings you made from the tuple and the list and counting the characters. In other words:

Python:
print(t)
print(L)
 
  • Like
Likes jedishrfu and Taylor_1989
  • #3
PeterDonis said:
Try printing the actual strings you made from the tuple and the list and counting the characters. In other words:

Python:
print(t)
print(L)

Ah I see the tuple and lists output spaces, thank you.
 
  • Like
Likes jedishrfu
  • #4
Taylor_1989 said:
the tuple and lists output spaces

And the delimiters as well, yes.
 
  • Like
Likes Taylor_1989 and jedishrfu

1. How does the str() function convert a list into a string?

The str() function converts a list into a string by joining all the elements in the list together, separated by a specified delimiter. The resulting string will have each element of the list represented as a string, with the delimiter in between each element.

2. Can the str() function also convert a tuple into a string?

Yes, the str() function can also convert a tuple into a string. The process is the same as converting a list, where the elements of the tuple will be joined together with the specified delimiter.

3. What happens if the elements in the list or tuple are not strings?

If the elements in the list or tuple are not strings, the str() function will automatically convert them into strings before joining them together. This means that the resulting string may contain a mix of string and non-string elements.

4. Can I specify a different delimiter when using the str() function?

Yes, the str() function allows you to specify a different delimiter to use when converting a list or tuple into a string. By default, it uses an empty string as the delimiter, but you can pass in any string you want to use as the delimiter.

5. Is there a limit to the number of elements that can be converted using the str() function?

No, there is no limit to the number of elements that can be converted using the str() function. It will join all the elements together, regardless of how many there are. However, keep in mind that the resulting string may become very long if there are a large number of elements.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
652
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
3
Views
323
  • Programming and Computer Science
Replies
1
Views
285
  • Programming and Computer Science
2
Replies
43
Views
3K
  • Programming and Computer Science
Replies
10
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
Back
Top