Electron microscopy
 
Check if a String is Empty, NaN Value or Space
- Python for Integrated Circuits -
- An Online Book -
Python for Integrated Circuits                                                                                   http://www.globalsino.com/ICs/        


Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

=================================================================================

We can check if a string is empty, contains only whitespace characters, or is NaN in Python using various methods:

  • Checking for Empty String: Use the len() function to check the length of the string. Code

     

    Output: 

     

  • Checking for Whitespace Characters: Use the isspace() method to check if the string contains only whitespace characters. Code

    Output: 

  • Checking for NaN Value: There's no built-in concept of "NaN" for strings in Python like there is for numeric types. However, if we are dealing with NaN values as strings (e.g., if we have loaded data from a file or received it as input), we can simply check if the string is equal to "NaN". Code

     

    Output: 

     

  • If we are dealing with a single string value and want to convert it to 0 if it’s either NaN or an empty string; otherwise, it returns the original value, then we can use the code:

    Output:

============================================ 

Check if a string is empty or not (space is not considered as empty): Code:
         Upload Files to Webpages  
       Output (The marked below are spaces):    
         Upload Files to Webpages

============================================

Get definite an answer about string empty by removing spaces first: Code:
         Upload Files to Webpages  
       Output (The marked below are spaces):    
         Upload Files to Webpages

============================================

Check if a string is empty or space: Code:
         Upload Files to Webpages  
       Output (The marked below are spaces):    
         Upload Files to Webpages

============================================

Check if a string is in NaN value: Code:
         Upload Files to Webpages  
       Output:    
         Upload Files to Webpages

============================================

         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         

 

 

 

 

 



















































 

 

 

 

 

=================================================================================