• Register
0 votes
19 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,060 points

Please log in or register to answer this question.

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,950 points

Related questions

0 votes
1 answer 24 views
24 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, 2020 LizzyM 6.1k points
1 vote
1 answer 31 views
31 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, 2020 LizzyM 6.1k points
1 vote
1 answer 13 views
13 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, 2020 samhaz 5k points
1 vote
1 answer 586 views
586 views
Problem: I am getting the following error : error: array initializer must be an initializer list or string literal For the array; char x[80] = GetString(); Can any body explain or help?
asked Apr 2, 2020 LizzyM 6.1k points
0 votes
1 answer 10 views
10 views
Problem: I've got some Python code that runs through a list of strings and converts them to integers or floating point numbers if possible. Doing this for integers is pretty easy if element.isdigit(): newelement = int(element) Floating point numbers are more difficult. Right now I'm using partition('.') to split the string and checking to make sure that one or both sides are digits.
asked 3 days ago Mashhoodch 9.1k points
1 vote
1 answer 58 views
58 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, 2020 samhaz 5k points
0 votes
1 answer 43 views
43 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, 2020 Gavin 15.3k points
1 vote
3 answers 56 views
56 views
Problem: I was trying to check if string contains substring from list using python. So I wrote this code which gives me wrong results. The code I wrote is: mainstring = "Allinall" substring = "all" if substring is mainstring: print ("String Found") else: print (" ... the answer is not right. What is wrong in my code. Cam anyone explain and how can I check if string contains substring from list.
asked Apr 22, 2020 samhaz 5k points
1 vote
1 answer 575 views
575 views
Problem: Hello Kodlogs, I am sure you guys are doing well. I just entered the world of Python programming. I&rsquo;ve got a problem to solve that says, &ldquo;python check if string contains only certain characters&ldquo; I tried several methods. Unfortunately, none ... worked for me. Could you please write a program that can check if a string contains only certain characters? Thanks in advance.
asked Mar 31, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
Problem: Python String isnumeric() Method The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like &sup2; and &frac34; are also considered to be numeric values. then how check if a string is a number python
asked 11 hours ago Mashhoodch 9.1k points