How to check if the string is empty?

后端 未结 25 1586
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 14:47

Does Python have something like an empty string variable where you can do:

if myString == string.empty:

Regardless, what\'s the most elegan

25条回答
  •  花落未央
    2020-11-22 15:07

    Below is a simple method to identify an empty string:

    a="" #Empty string
    if(len(a)==0): #len() is used to find the length of the string or variable inside its' brackets.
       print("Empty")
    else:
       print("Filled")
    

    The above len() method is an inbuilt function that helps us to find the length of a certain string or variable. It can also be used in the case of lists.

    LOGIC: If the length of the character or variable is 0 then absolutely it means that the string or variable is empty. BUT remember that this will work only in the case of strings and lists and not with integers. Below is the code to find length of integers.

    a=int(input())
    if(len(str(a))==0):
       print(True)
    

    In the above code str() is used to convert an int to a string format like from 123 to its string format "123". In the string format it will be valid.

提交回复
热议问题