Today, I am going to tell a story about my copy & paste mistake and how a string with trailing comma get coupled and became a Tuple() in my Python program. Strangely, I was not not aware of this concept before making this mistake.
So, let start with having a quick learning about string and tuple in Python.
Definition: A sequence of characters used to store and manipulate text.
Syntax: Enclosed in single quotes ' ', double quotes " ", or triple quotes ''' '''/""" """.
Immutability: Strings cannot be changed after creation.
# Example mystring = "Hello, World!" print(type(mystring)) #Output:mystring2 =str() print(type(mystring2)) #Output:
Definition: An immutable sequence of values.
Syntax: Defined with parentheses ( ), separated by commas.
Immutability: Once created, elements cannot be changed.
# Example mytuple = (1,2) print(type(mytuple)) #Output:mytuple2 =tuple() print(type(mytuple2)) #Output:
Now, we know about string and tuple and how to get a variable of type string or tuple using either direct assignment or using class approach by using constructor. But, my story is different :).
Python statement do not need a semicolon (;) or comma (,) or any other closing symbol (spacing is used to handle block), so how you can end-up a trailing comma with string like “Ajit Kumar”, because often you copy string element from list of dictionary and often endup with a trailing comma. This is what happened to me and then story became interesting.
So, first let have an example of my stroy.
mymistake = "Ajit Kumar", print(type(mymistake)) #Output :
Now, there are two things to observe:
1) Python interpreter will not throw any error due to the trailing comma with the string 'mymistake', otherwise error would have not be go any further.
2) The variable type became 'tuple' than original intention of creating a string variable mymistake.
I wrote here and it seems very easy and simple, but it took hour to debug this error, because i was passing the string as argument to a method and the errors were strange and i keep working on method and trying to understand the error message. For example, the message was like, the 'name' variable don't have a method lower().
If you use a comma without parentheses, Python treats it as a tuple.
Example: mymistake = "Ajit Kumar", creates a tuple ("Ajit Kumar",).
So, if you want to create a tuple having only item/element, you need to be very careful. Following example, demonstrate that, in first example, even an item is within pair of brackets (1), python treat that as integer and not tuple.
singleitem = (1) print(type(singleitem)) #Output:
So, to create tuple with a single item , you must need to use a trailing comma, as follows:
singleitemtuple = (1,) print(singleitemtuple) #output: (1,) print(type(singleitemtuple)) #output:
And, i think, this is the reason why when the string having a trailing comma so python takes that as tuple and created the variable as tuple not as string.
However, the Python documentation have mentioned it clearly and it was my mistake or my negligence that i didn't notice it and have been always using () to create a tuple even for a single item with training comma. Source
Anyway, this given me some new learning and a shortcut to create a tuple with single item, or more items without using (). Yes, I went ahead try to use the "trailing comma" with more items to create a tuple and it worked. Example:
mylearning = "Ajit Kumar","April","python", print(mylearning) #output: ('Ajit Kumar', 'April', 'python') print(type(mymistake)) #output:
It was a lot of learning on weekend. I hope, it will be helpful for you. Put your discussion and suggestion in the comment. I love to hear about Python shortcuts and out-of-box features.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3