• Register
0 votes
12 views

I am new to python programming.. I was trying to check if a string is empty or whitespace in a code :

Mystr= ""
Yourstr = "   "

print ("This string is empty : ", end = "") 
if(not Yourstr): 
    print ("Yes") 

  
  
print ("This string is empty : ", end = "") 
if(not Mystr): 
    print ("No") 

 It’s output is :

This string is empty : This string is empty : No

Its not giving me actual result what can I do now?

10 7 2
6,050 points

1 Answer

0 votes

In your code you should have used else statement with your if sattement. You should write your code like:

Mystr= ""
Yourstr = "   "

print ("This string is empty : ", end = "") 
if(not Mystr): 
    print ("Yes") 
else : 
    print ("No") 
  
  
print ("This string is empty : ", end = "") 
if(not Yourstr): 
    print ("Yes") 
else : 
    print ("No") 

and your ouput will be:

This string is empty : Yes
This string is empty : No

 

11 6 4
34,930 points

Related questions

0 votes
1 answer 16 views
16 views
I am new to python programming I was trying to check if a letter is uppercase in python. I wrote some code to check this statement : import string def UpperCase(s_str): for x in s_str: if x not in string.UpeerCase: return False return True But I can not get the actual ... last): File "<stdin>", line 1, in <module> NameError: name 'LoWWE' is not defined Can anyone help me to solve this thing?
asked Mar 14 LizzyM 6.1k points
1 vote
1 answer 11 views
11 views
Problem: For the following code where I was checking a string that consists a empty string python: Firststring= "" Secoendstring = " " print ("Your string is empty : ", end = "") if(not Secoendstring): print ("Yes") print ("Your string is empty : ", end = "") if(not ... . Can any one tell me why am I getting this type of result and how can I fix my code to get the exact result. Thanks in advance.
asked May 4 samhaz 5k points
1 vote
1 answer 22 views
22 views
Problem: I am new in pyhton programing language . I was trying to check if two strings are anagrams python. But I fail repitedly. The code I wrote to check if two strings are anagrams is: def IsAnagram(str1,str2): str1 = input("Enter your first string : ") ... , line 4 str1 = input("Enter your first string : ") ^ IndentationError: expected an indented block What is the problem can anyone tell?
asked Apr 2 LizzyM 6.1k points
1 vote
1 answer 38 views
38 views
Problem: I was trying to check if string contains special characters using a python program. To check this I wrote this code: import re # Function checks if the string # contains any special character def check(string): regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]' ... line 11, in check if(search(string) == None): NameError: name 'search' is not defined How can I solve this problem can anyone help?
asked Jun 10 samhaz 5k points
0 votes
1 answer 31 views
31 views
Problem: Hello Kodlogs, I am a beginner to python and I am having a doubt that how to check if the character is present in the string or not though, I know one method but wondering if any other method is available or not (in operator ) this is the method I am using ... ) print("Check if "+string+" contains "+subchar1+":") print(subchar1 in string) Waiting for a few more method that can help me.
asked Jun 5 Gavin 15.3k points