Hướng dẫn how do you check if a string is just spaces python? - làm thế nào để bạn kiểm tra xem một chuỗi có phải là dấu cách không python?

str.isspace () trả về

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
0 cho chuỗi hợp lệ và trống

>>> tests = ['foo', ' ', '\r\n\t', '']
>>> print([s.isspace() for s in tests])
[False, True, True, False]

Do đó, kiểm tra với

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
1 cũng sẽ đánh giá loại
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
2 và
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
3 hoặc
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
4 (chuỗi trống)

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
The original string is : Geeks  forGeeks
Does string contain spaces ? True
1

The original string is : Geeks  forGeeks
Does string contain spaces ? True
2
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
0

Cải thiện bài viếtreturns “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:

  • Lưu bài viết
  • Đôi khi, chúng ta có thể gặp vấn đề trong đó chúng ta cần kiểm tra xem chuỗi có bất kỳ khoảng trống nào không. Loại vấn đề này có thể nằm trong miền học máy để có được loại dữ liệu cụ thể. Hãy để thảo luận về những cách nhất định trong đó loại vấn đề này có thể được giải quyết. Phương pháp số 1: Sử dụng Regex, loại vấn đề này có thể được giải quyết bằng cách sử dụng tiện ích regex do Python cung cấp. Bằng cách cung cấp chuỗi regex thích hợp trong search (), chúng ta có thể kiểm tra sự hiện diện của không gian trong một chuỗi. & Nbsp;
  • The original string is : Geeks forGeeks
    Does string contain spaces ? True
    3
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    4
  • The original string is : Geeks forGeeks
    Does string contain spaces ? True
    5
    >>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
    >>> print ([not s or s.isspace() for s in tests])
    [False, True, True, True, True, True]
    
    6
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    7
  • >>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
    >>> print ([not s or s.isspace() for s in tests])
    [False, True, True, True, True, True]
    
    8
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    9
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    0
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    1
    3
    8
    4
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    3
  • The original string is : Geeks forGeeks
    Does string contain spaces ? True
    4
    >>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
    >>> print ([not s or s.isspace() for s in tests])
    [False, True, True, True, True, True]
    
    6
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    6
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    7

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""] >>> print ([not s or s.isspace() for s in tests]) [False, True, True, True, True, True] 8The original string is : Geeks forGeeks Does string contain spaces ? True93 84 >>> tests = ['foo', ' ', '\r\n\t', '', None, ""] >>> print ([not s or s.isspace() for s in tests]) [False, True, True, True, True, True] 01>>> tests = ['foo', ' ', '\r\n\t', '', None, ""] >>> print ([not s or s.isspace() for s in tests]) [False, True, True, True, True, True] 02

Đầu ra: & nbsp;string.isspace()

Returns:

  1. Phương pháp số 2: Sử dụng toán tử trong toán tử, tác vụ này cũng có thể được thực hiện bằng toán tử. Chỉ cần kiểm tra một không gian trong chuỗi. Phán quyết được trả lại là đúng ngay cả khi một không gian duy nhất được tìm thấy và sai nếu không. & Nbsp;– If all characters in the string are whitespace characters.
  2. The original string is : Geeks forGeeks
    Does string contain spaces ? True
    4
    >>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
    >>> print ([not s or s.isspace() for s in tests])
    [False, True, True, True, True, True]
    
    6
    >>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
    >>> print ([not s or s.isspace() for s in tests])
    [False, True, True, True, True, True]
    
    14
    Input : string = 'My name is Ayush'
    Output : 3
    
    Input : string = 'My name is \n\n\n\n\nAyush'
    Output : 8
    3
    >>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
    >>> print ([not s or s.isspace() for s in tests])
    [False, True, True, True, True, True]
    
    16
    – If the string contains 1 or more non-whitespace characters.

Phương thức số 3: Sử dụng phương thức Find ().

Python3

Tìm () Phương thức Tìm kiếm chuỗi đã cho cho đối số được truyền và trả về vị trí hoặc trả về -1.

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
9

Output:

True

The original string is : Geeks forGeeks Does string contain spaces ? True5>>> tests = ['foo', ' ', '\r\n\t', '', None, ""] >>> print ([not s or s.isspace() for s in tests]) [False, True, True, True, True, True] 6 >>> tests = ['foo', ' ', '\r\n\t', '', None, ""] >>> print ([not s or s.isspace() for s in tests]) [False, True, True, True, True, True] 24

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
26
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
27
3
8
4
The original string is : Geeks forGeeks
Does string contain spaces ? True
3

Python3

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
True
2

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
9

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
True
7

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
9

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
False
True
False
2

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
False
True
False
4

Output: 

False
True
False

Ví dụ 2: Ứng dụng thực tếApplication

Cho một chuỗi trong Python, đếm số lượng ký tự khoảng trắng trong chuỗi. & NBSP;

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8

Algorithm:  

  1. Đi qua ký tự chuỗi đã cho bằng ký tự lên đến chiều dài của nó, kiểm tra xem ký tự là ký tự khoảng trắng. & NBSP;
  2. Nếu đó là một ký tự khoảng trắng, hãy tăng bộ đếm 1, khác đi qua ký tự tiếp theo. & NBSP;
  3. In giá trị của bộ đếm.

Python3

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
False
True
False
7

False
True
False
8
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
0

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
1
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
2
Input : string = 'My name is Ayush'
Output : 3

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
4

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
5
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
6
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
7
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
3
8
0
3
8
1

3
8
2
False
True
False
8
3
8
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
3
8
6

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
3
8
8

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
The original string is : Geeks  forGeeks
Does string contain spaces ? True
1

The original string is : Geeks  forGeeks
Does string contain spaces ? True
2
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
0

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
1
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
2
Input : string = 'My name is Ayush'
Output : 3

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
4

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
5
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
6
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
7
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
3
8
0
3
8
1

3
8
2
False
True
False
8
3
8
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
3
8
6

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
3
8
8

Output: 

3
8

Cải thiện bài viết

Lưu bài viết

Đôi khi, chúng ta có thể gặp vấn đề trong đó chúng ta cần kiểm tra xem chuỗi có bất kỳ khoảng trống nào không. Loại vấn đề này có thể nằm trong miền học máy để có được loại dữ liệu cụ thể. Hãy để thảo luận về những cách nhất định trong đó loại vấn đề này có thể được giải quyết. Phương pháp số 1: Sử dụng Regex, loại vấn đề này có thể được giải quyết bằng cách sử dụng tiện ích regex do Python cung cấp. Bằng cách cung cấp chuỗi regex thích hợp trong search (), chúng ta có thể kiểm tra sự hiện diện của không gian trong một chuỗi. & Nbsp;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

The original string is : Geeks forGeeks
Does string contain spaces ? True
3
The original string is : Geeks forGeeks
Does string contain spaces ? True
4

The original string is : Geeks forGeeks
Does string contain spaces ? True
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
The original string is : Geeks forGeeks
Does string contain spaces ? True
7

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
The original string is : Geeks forGeeks
Does string contain spaces ? True
9
The original string is : Geeks forGeeks
Does string contain spaces ? True
0
The original string is : Geeks forGeeks
Does string contain spaces ? True
1
3
8
4
The original string is : Geeks forGeeks
Does string contain spaces ? True
3

The original string is : Geeks forGeeks
Does string contain spaces ? True
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
The original string is : Geeks forGeeks
Does string contain spaces ? True
6
The original string is : Geeks forGeeks
Does string contain spaces ? True
7

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
The original string is : Geeks forGeeks
Does string contain spaces ? True
9
3
8
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
01
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
02

Đầu ra: & nbsp;

The original string is : Geeks  forGeeks
Does string contain spaces ? True

Phương pháp số 2: Sử dụng toán tử trong toán tử, tác vụ này cũng có thể được thực hiện bằng toán tử. Chỉ cần kiểm tra một không gian trong chuỗi. Phán quyết được trả lại là đúng ngay cả khi một không gian duy nhất được tìm thấy và sai nếu không. & Nbsp; 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

The original string is : Geeks forGeeks
Does string contain spaces ? True
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
The original string is : Geeks forGeeks
Does string contain spaces ? True
7

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
The original string is : Geeks forGeeks
Does string contain spaces ? True
9
The original string is : Geeks forGeeks
Does string contain spaces ? True
0
The original string is : Geeks forGeeks
Does string contain spaces ? True
1
3
8
4
The original string is : Geeks forGeeks
Does string contain spaces ? True
3

The original string is : Geeks forGeeks
Does string contain spaces ? True
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
The original string is : Geeks forGeeks
Does string contain spaces ? True
6
The original string is : Geeks forGeeks
Does string contain spaces ? True
7

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
The original string is : Geeks forGeeks
Does string contain spaces ? True
9
3
8
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
01
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
02

Đầu ra: & nbsp;

The original string is : Geeks  forGeeks
Does string contain spaces ? True

Phương pháp số 2: Sử dụng toán tử trong toán tử, tác vụ này cũng có thể được thực hiện bằng toán tử. Chỉ cần kiểm tra một không gian trong chuỗi. Phán quyết được trả lại là đúng ngay cả khi một không gian duy nhất được tìm thấy và sai nếu không. & Nbsp;.

The original string is : Geeks forGeeks
Does string contain spaces ? True
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
14
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
3
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
16

Python3

Phương thức số 3: Sử dụng phương thức Find ().

Tìm () Phương thức Tìm kiếm chuỗi đã cho cho đối số được truyền và trả về vị trí hoặc trả về -1.

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
30
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
0

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
34
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
35
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
36
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
38
3
8
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
40

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
30
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
3
8
0

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
26
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
47
3
8
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
01
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
02

Đầu ra

The original string is : Geeks forGeeks
Does string contain spaces ? True

The original string is : Geeks forGeeks
Does string contain spaces ? True
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
24

 

Python3

Phương thức số 3: Sử dụng phương thức Find ().

Tìm () Phương thức Tìm kiếm chuỗi đã cho cho đối số được truyền và trả về vị trí hoặc trả về -1.

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
59
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
0

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
30
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
0

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
1
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
66
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
3
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
68

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
5
Input : string = 'My name is Ayush'
Output : 3

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
71

3
8
2
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
59
3
8
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
3
8
6

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
78
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
3
8
6
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
40

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

Input : string = 'My name is \n\n\n\n\nAyush'
Output : 8
5
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
30
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
6
3
8
0

>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
8
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
26
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
47
3
8
4
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
01
>>> tests = ['foo', ' ', '\r\n\t', '', None, ""]
>>> print ([not s or s.isspace() for s in tests])
[False, True, True, True, True, True]
02

Đầu ra

The original string is : Geeks forGeeks
Does string contain spaces ? True