How to check if a character is whitespace in Python

2 Answers

0 votes
import string  

ch = " "
if ch in string.whitespace:
    print("yes")
else:
    print("no")
    

ch = "\n"
if ch in string.whitespace:
    print("yes")
else:
    print("no")
    
    
ch = "\t"
if ch in string.whitespace:
    print("yes")
else:
    print("no")
 
 
ch = "_"
if ch in string.whitespace:
    print("yes")
else:
    print("no")
    
    
 
     
'''
run:
 
yes
yes
yes
no

'''

 



answered May 17, 2019 by avibootz
0 votes
import string
 
if ' ' in string.whitespace:
    print("yes")
 
if '\t' in string.whitespace:
    print("yes")
 
if '\n' in string.whitespace:
    print("yes")
 
if 'a' in string.whitespace:
    print("yes")
else:
    print("no")
 

 
'''
run:
   
yes
yes
yes
no
 
'''

 



answered May 16, 2024 by avibootz

Related questions

1 answer 297 views
1 answer 195 views
1 answer 245 views
1 answer 293 views
1 answer 349 views
...