View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Sometimes, we might have a problem in which we need to check if the string has any of blank spaces. This kind of problem can be in Machine Learning domain to get specific type of data set. Let’s discuss certain ways in which this kind of problem can be solved. Method #1 : Using regex This kind of problem can be solved using the regex utility offered by python. By feeding the appropriate regex string in search[], we can check presence of space in a string.
Python3
import
re
test_str
=
"Geeks forGeeks"
print
["The original string
is
: "
+
test_str]
res
=
bool
[re.search[r"\s", test_str]]
print
["Does string contain spaces ? "
+
str
[res]]
Output :
The original string is : Geeks forGeeks Does string contain spaces ? True
Method #2 : Using in operator This task can also be performed using in operator. Just required to check for a space in the string. The verdict returned is true even if a single space is found and false otherwise.
Python3
test_str
=
"Geeks forGeeks"
print
["The original string
is
: "
+
test_str]
res
=
" "
in
test_str
print
["Does string contain spaces ? "
+
str
[res]]
Output :
The original string is : Geeks forGeeks Does string contain spaces ? True
Method #3: Using find[] method.
find[] method searches the given string for argument passed and returns the position or else returns -1.
Python3
test_str
=
"Geeks forGeeks"
print
[
"The original string is : "
+
test_str]
res
=
False
if
[test_str.find[
" "
]!
=
-
1
]:
res
=
True
print
[
"Does string contain spaces ? "
+
str
[res]]
Output
The original string is : Geeks forGeeks Does string contain spaces ? True
Method #4: Using isspace[] method
Python3
test_str
=
"Geeks forGeeks"
print
[
"The original string is : "
+
test_str]
c
=
0
res
=
False
for
i
in
test_str:
if
[i.isspace[]]:
c
+
=
1
if
[c>
=
1
]:
res
=
True
print
[
"Does string contain spaces ? "
+
str
[res]]
Output
The original string is : Geeks forGeeks Does string contain spaces ? True
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Python String isspace[] method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters, such as:
- ‘ ‘ – Space
- ‘\t’ – Horizontal tab
- ‘\n’ – Newline
- ‘\v’ – Vertical tab
- ‘\f’ – Feed
- ‘\r’ – Carriage return
Python String isspace[] Method Syntax
Syntax: string.isspace[]
Returns:
- True – If all characters in the string are whitespace characters.
- False – If the string contains 1 or more non-whitespace characters.
Python String isspace[] Method Example
Python3
string
=
"\n\t\n"
print
[string.isspace[]]
Output:
True
Example 1: Basic Intuition of isspace[] in Program
Here we will check whitespace in the string using isspace[] program.
Python3
string
=
'Geeksforgeeks'
print
[string.isspace[]]
string
=
'\n \n \n'
print
[string.isspace[]]
string
=
'Geeks\nfor\ngeeks'
print
[ string.isspace[]]
Output:
False True False
Example 2: Practical Application
Given a string in Python, count the number of whitespace characters in the string.
Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 8
Algorithm:
- Traverse the given string character by character up to its length, check if the character is a whitespace character.
- If it is a whitespace character, increment the counter by 1, else traverse to the next character.
- Print the value of the counter.
Python3
string
=
'My name is Ayush'
count
=
0
for
a
in
string:
if
[a.isspace[]]
=
=
True
:
count
+
=
1
print
[count]
string
=
'My name is \n\n\n\n\nAyush'
count
=
0
for
a
in
string:
if
[a.isspace[]]
=
=
True
:
count
+
=
1
print
[count]
Output:
3 8