Python String isspace () and its application

Counters | isspace | Python Methods and Functions | String Variables

In Python, isspace () — it is a built-in method used to process strings. The isspace () methods return True if all characters in the string are whitespace, otherwise it returns False. 
This function is used to check if the argument contains all whitespace characters, such as:

  • & # 39; & # 39; — Space
  • & # 39; / t & # 39; — Horizontal tab
  • & # 39; / n & # 39; — Newline
  • & # 39; / v & # 39; — vertical tab
  • & # 39; / f & # 39; — Feed
  • & # 39; / r & # 39; — carriage return

Syntax :

  string.isspace ()   Parameters:  isspace () does not take any parameters  Returns:  1.True- If all characters in the string are whitespace characters. 2.False- If the string contains 1 or more non-whitespace characters. 

Examples :

 Input: string = 'Geeksforgeeks' Output: False Input: string =' 'Output: True Input: string =' Geeks For Geeks 'Output: False 

# Python code to implement isspace ()

  
# whitespace checking

string = ' Geeksforgeeks'

 

print (string.isspace ())

 
# check if / n is a space character

string = ''

 

print (string.isspace ())

  

string = 'Geeks for geeks'

print (string.isspace ())

Output:

 False True False 

claim

For a given string in python, count the number of whitespace characters in the string. 
Example:

 Input: string = 'My name is Ayush' Output: 3 Input: string =' My name is Ayush' Output: 8 

Algorithm
1. Go through the given line character by character to its length, check if the character is a space. 
2. If it is a whitespace character, increment the counter by 1, otherwise go to the next character. 
3. Print the counter value.

# Python implementation for counting whitespace in a string
# Specified string
# Initialize counter to 0

string = 'My name is Ayush'

count = 0

 
# Line iteration and whitespace checking
# Increment counter if whitespace character is found
# Finally, print the invoice

for a in string:

if (a.isspace ()) = = True :

count + = 1

print (count)

  

string = 'My name is Ayush'

count = 0

for a in string:

if (a.isspace ())   = = True :

count + = 1

print (count)

Output:

 3 8 




Python String isspace () and its application: 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