operations with empty strings | isspace () function

Arrays | isspace | NumPy | Python Methods and Functions | String Variables

numpy.core.defchararray.isspace (arr) returns true for each item if the string contains only whitespace and at least one character. Returns false otherwise.

Syntax: numpy.core.isspace (arr)

Parameters:
arr: array_like of str or unicode.

Returns: [ndarray] Output array of bools.

Code # 1:

# Python program explaining
# numpy.char.isspace () method

  

import numpy as geek

 
# input arrays without spaces

in_arr = geek.array ([ `Geeksforgeeks` , ` Codechef` ])

print ( " Input array: " , in_arr) 

 

out_arr = geek.char.isspace (in_arr)

print ( " Output array: " , out_arr)

Output :

 Input array: [`Geeksforgeeks`` Codechef`] Output array: [False False] 

Code # 2:

# Python program explaining
# numpy.char. isspace () method

 

import numpy as geek

 
# input arrays containing a string with a space

in_arr = geek.array ([ `Geeks for geeks` , `Code chef` ])

print ( "Input array:" , in_arr) 

 

out_arr = geek.char.isspace (in_arr)

print ( "Output array:" , out_arr)

Exit:

< pre> Input array: [`Geeks for geeks`` Code chef`] Output array: [False False]

Code # 3:

# Python program explaining
# numpy.char.isspace () method

 

import numpy as geek

 
# input arrays containing only spaces

in_arr = geek.array ([ ` ` , ` ` , ` ` , ` ` ])

print ( "Input array:" , in_arr) 

 

out_arr = geek.char.isspace (in_arr)

print ( "Output array:" , out_arr)

Output :

 Input array: [`` `` `` `] Output array: [True True True True] 




operations with empty strings | isspace () function: StackOverflow Questions

Answer #1

I once wrote something similar to Bartek"s answer and javascript inspired:

def is_not_blank(s):
    return bool(s and not s.isspace())

Test:

print is_not_blank("")    # False
print is_not_blank("   ") # False
print is_not_blank("ok")  # True
print is_not_blank(None)  # False

Answer #2

Use the str.isspace() method:

Return True if there are only whitespace characters in the string and there is at least one character, False otherwise.

A character is whitespace if in the Unicode character database (see unicodedata), either its general category is Zs (“Separator, space”), or its bidirectional class is one of WS, B, or S.

Combine that with a special case for handling the empty string.

Alternatively, you could use str.strip() and check if the result is empty.

Answer #3

Why not use title Right from the docs:

>>> "they"re bill"s friends from the UK".title()
"They"Re Bill"S Friends From The Uk"

If you really wanted PascalCase you can use this:

>>> "".join(x for x in "make IT pascal CaSe".title() if not x.isspace())
"MakeItPascalCase"

Get Solution for free from DataCamp guru