python if string has spaces 1

python if string has spaces

In [1]: import re

In [2]: string = "Hello world"

In [3]: bool(re.search('\s', string))
Out[3]: True

In [4]: string = "World"

In [5]: bool(re.search('\s', string))
Out[5]: False

Here is what the above code is Doing:
1. Import the re module
2. Create a string
3. Use the re.search() function to search for a space in the string.
4. If a space is found, the function returns a match object.
5. The bool() function returns True if the match object is not None.
6. If no space is found, the function returns None.
7. The bool() function returns False if the match object is None.

Similar Posts