6

I would like to understand if there is a difference between assigning an empty value and an empty output, as follows:

1> Assigning a value like this

string = ""

2> An empty value returned as output

string = "abcd:"
str1, str2 = split(':')

In other words, is there a difference in values of 'string' in 1> and 'str2' in 2>? And how would a method see the value of 'str2' if it is passed as an argument?

4
  • Why would there be a difference? Anyway, if you wanted to test this, you could just use == to find out. Feb 24, 2014 at 5:32
  • empty string is an empty string. Though, don't go comparing them with is :-)
    – roippi
    Feb 24, 2014 at 5:34
  • @roippi: is actually returns True in this case Feb 24, 2014 at 5:34
  • @DavidRobinson I know, I should say don't go comparing strings-in-general with is. We don't want more dupes of those questions :-)
    – roippi
    Feb 24, 2014 at 5:37

7 Answers 7

7

Checking equality with ==

>>> string = ""
>>> s = "abcd:"
>>> str1, str2 = s.split(':')
>>> str1
'abcd'
>>> str2
''
>>> str2 == string
True

Maybe you were trying to compare with is. This is for testing identity: a is b is equivalent to id(a) == id(b).

Or check both strings for emptiness:

>>> not str2
True
>>> not string
True
>>> 

So that both are empty ...

2
>>> string1 = ""
>>> string2 = "abcd:"
>>> str1, str2 = string.split(':')
>>> str1
'abcd'
>>> str2
''
>>> string1 == str2
True

No. There is no difference between the two empty strings. They would behave the same in all cases.

2

In other words, is there a difference in values of 'string' in 1> and 'str2' in 2>?

No, there is no difference, both are empty strings "".

And how would a method see the value of 'str2' if it is passed as an argument?

The method would see it as a string of length 0, in other words, an empty string.

2

If you will check id(string) in case-1 and id(str2) in case2, it will give u the same value, both the string objects are same.

def mine(str1, str2):
    print str1, str2

see the above method you can call mine(* string.split(':')) it will pass the 'abcd:' as str1 = 'abcd' and str2 = ''.

1

You can see for yourself.

>>> s1 = ''
>>> s2 = 'abcd:'
>>> s3, s4 = s2.split(':')
>>> s1 == s4
True
1
>>> string = ""
>>> id(string)
2458400
>>> print string

>>> string = "abcd:"
>>> str1, str2 = string.split(':')
>>> print str1
abcd
>>> print str2

>>> id(str2)
2458400
>>> type(string)
<type 'str'>
>>> type(str2)
<type 'str'>

No there is no difference

1

Empty string is a literal, in Python literals are immutable objects and there value never changes. However, in some cases two literal objects having same value can have different identities (Identity of an object is an address of the memory location in CPython and you can get it by using id(obj)) so to answer your question

print id(string) == id(str2)  # Can output either True or False
print string == str2  # Will always output True

Note that most of the time id(string) should be equal to id(str2) :).

You can read about the Data Model in the Python Language Reference for further details. I am quoting the text which is pertinent to the question:

Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation, but after c = []; d = [], c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c and d.)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.