Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Check if any String is empty in list
Next article icon

We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example:

Using Comparison Operator(==)

The simplest and most commonly used approach is using comparison operator(==):

Python
s=""ifs=="":print("The string is empty.")else:print("The string is not empty.")

Output
The string is empty.

Explanation: A simple equality check (s == "") determines if the string is empty.

Using len()

The len()function returns the length of a string so if the length is 0, it means that string is empty:

Python
s=""iflen(s)==0:print("The string is empty.")else:print("The string is not empty.")

Output
The string is empty.

Explanation:

  • len(s): calculates the length ofs.
  • Iflen(s) == 0 then the string is empty.

Using Python's Truthy/Falsy Behavior

In Python, empty strings are treated as "Falsy". So, we can use this for checking if a string is empty or not.

Python
s=""ifnots:print("The string is empty.")else:print("The string is not empty.")

Output
The string is empty.

Explanation: "not s"negates the boolean value ofs, if it's empty then it's boolean quivalent to False and its negation would be True.


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp