Python: difference assignment a = " " and a = [ ] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Python: difference assignment a = " " and a = [ ]

Can anyone tell me the difference between assigning quotes or square brackets to a variable? Example: a = "" a = [] Thank you

19th Apr 2018, 8:51 PM
Antonio Bernardini
Antonio Bernardini - avatar
8 Answers
+ 7
a = "" is assigning an empty string literal to variable a a = [] is assigning an empty list
19th Apr 2018, 8:57 PM
Eligijus Silkartas
Eligijus Silkartas - avatar
+ 6
Let's say you want to add words to a string to create a longer string. You can start with an empty String a = ""... It is a bit like when we initialize an integer to 0 and then start adding stuff to it like in the case of using a loop to get the sum of the elements of an array... That is just one example of how an empty String can be helpful
19th Apr 2018, 9:03 PM
cyk
cyk - avatar
+ 4
This is in Java. Would it be easier to understand the same example in another language? Let me know if it helps ☺ https://code.sololearn.com/cluTfBpN34SR/?ref=app
19th Apr 2018, 10:05 PM
cyk
cyk - avatar
+ 2
a="" means that a string is assigned to a. And a=[] means to assign an array. This is a kind of hint given to interpreter about the data kind to be stored in the variable. Extra tips: a = 0 #assign a numerical value a = false #assign a boolean value
27th Jul 2018, 6:13 AM
Seniru
Seniru - avatar
+ 1
but in the case of a = "", how is it used in practice?
19th Apr 2018, 8:59 PM
Antonio Bernardini
Antonio Bernardini - avatar
+ 1
a simple program with which I can understand?
19th Apr 2018, 9:06 PM
Antonio Bernardini
Antonio Bernardini - avatar
+ 1
a = "" = a = str() You can use this e.g. to declare a variable in order to avoid the classic "reference variable before assignment" error.
23rd Apr 2018, 7:08 AM
Johannes
Johannes - avatar
+ 1
Antonio Bernardini you can use it for append some chars using for loops for sometimes or anything related to string modification
27th Jul 2018, 6:15 AM
Seniru
Seniru - avatar