String and Trailing comma, get couple and become, Tuple (): A copy & paste mistake to Error and concept

WBOY
Release: 2024-07-21 22:43:21
Original
825 people have browsed it

String and Trailing comma, get couple and become, Tuple (): A copy & paste mistake to Error and concept

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.

String

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: <class 'str'>

mystring2 =str()
print(type(mystring2))
#Output: <class 'str'>
Copy after login

Tuple

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:<class 'tuple'>

mytuple2 =tuple()
print(type(mytuple2))
#Output:<class 'tuple'>
Copy after login

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.

String with Trailing comma

So, first let have an example of my stroy.

mymistake = "Ajit Kumar",
print(type(mymistake))
#Output : <class 'tuple'>
Copy after login

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().

Issues

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: <class 'int'>
Copy after login

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:  <class 'tuple'>
Copy after login

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:  <class 'tuple'>

Copy after login

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.

The above is the detailed content of String and Trailing comma, get couple and become, Tuple (): A copy & paste mistake to Error and concept. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!