Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning empty value or string in Python

Tags:

python

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?

like image 833
Maddy Avatar asked Feb 24 '14 05:02

Maddy


People also ask

How do you assign a blank value to a string in Python?

To initialize an empty string, we can use the string literal syntax double quotation marks "" in Python.

How do you declare an empty string?

In C#, you can use strings as an array of characters, However, more common practice is to use the string keyword to declare a string variable. The string keyword is an alias for the System. String class. To declare an empty string.

Is empty string Python?

Use len to Check if a String in Empty in Python # Using len() To Check if a String is Empty string = '' if len(string) == 0: print("Empty string!") else: print("Not empty string!") # Returns # Empty string! Keep in mind that this only checks if a string is truly empty.

Is null better than empty string?

NULL or Empty String: Which One to Use An empty string is a string instance of zero length. However, a NULL has no value at all. We can use a test database and apply the knowledge in the production environment to understand the concept better.


1 Answers

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

like image 183
kiriloff Avatar answered Nov 03 '22 02:11

kiriloff