Python Converting List and tuples using str() function

AI Thread Summary
The discussion revolves around converting tuples and lists to strings in Python and understanding the difference in their string representations. The user initially used the str() function to convert a tuple and a list into strings and noticed that the lengths of these strings were both 15, while a manually defined string had a length of 9. The discrepancy arises because the string representations of the tuple and list include additional characters such as spaces and delimiters (commas and parentheses), which contribute to the longer length. The user confirmed this by printing the actual strings derived from the tuple and list, leading to the realization of the extra characters present in their outputs.
Taylor_1989
Messages
400
Reaction score
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
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
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
Taylor_1989 said:
the tuple and lists output spaces

And the delimiters as well, yes.
 
  • Like
Likes Taylor_1989 and jedishrfu
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
2
Views
969
Replies
4
Views
2K
Replies
18
Views
1K
Replies
43
Views
4K
Replies
10
Views
5K
Replies
4
Views
2K
Replies
16
Views
3K
Back
Top