Làm cách nào để làm trống một chuỗi trong Python?

Sử dụng len() là phương pháp chung nhất để kiểm tra chuỗi có độ dài bằng không. Mặc dù nó bỏ qua thực tế là một chuỗi chỉ có khoảng trắng cũng thực tế được coi là chuỗi rỗng ngay cả khi nó khác không

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
9

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
0

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7_______10_______2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
0
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

['yes']
0_______30_______1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
['yes']
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

 

['yes']
7

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The string is empty.
0
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7_______10_______2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9
The string is empty.
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

['yes']
0_______30_______1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
['yes']
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

Đầu ra

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No

Phương pháp #2. sử dụng không

toán tử not cũng có thể thực hiện tác vụ tương tự như len() và kiểm tra chuỗi có độ dài 0, nhưng giống như ở trên, nó coi chuỗi chỉ có khoảng trắng cũng không trống, điều này thực tế không đúng.   

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1

string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
6

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
9

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
0

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2_______10_______3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7_______10_______2
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
8_______10_______9

['yes']
0
['yes']
1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
['yes']
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

 

['yes']
7

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2_______40_______0
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7_______10_______2
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
17

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
8_______10_______9

['yes']
0
['yes']
1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
['yes']
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

Đầu ra

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No

Phương pháp #3. Sử dụng not + str. dải()

Vấn đề về chuỗi trống + độ dài bằng 0 có thể được loại bỏ bằng cách sử dụng dải (), dải () trả về true nếu nó gặp khoảng trắng, do đó việc kiểm tra nó có thể giải quyết vấn đề kiểm tra chuỗi hoàn toàn trống.  
 

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
32

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
9

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
0

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2_______10_______3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7_______10_______2
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
50
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
51
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
52

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
8_______10_______9

['yes']
0
['yes']
1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
['yes']
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
65

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2_______40_______0
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7_______10_______2
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
75
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
51
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
77

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
8_______10_______9

['yes']
0
['yes']
1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
['yes']
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

Đầu ra

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes

Phương pháp #4. Sử dụng not + str. không gian

Hoạt động theo cách tương tự như phương pháp trên và kiểm tra khoảng trắng trong chuỗi. Phương pháp này hiệu quả hơn bởi vì, dải () yêu cầu thực hiện thao tác dải cũng cần tải tính toán, nếu không. không gian có số lượng tốt

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
90

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
92

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
9

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
0

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2_______10_______3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7_______10_______2
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
50
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
51
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
13

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
8_______10_______9

['yes']
0
['yes']
1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
['yes']
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
65

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2_______40_______0
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
4
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7_______10_______2
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
75
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
51
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
39

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
8_______10_______9

['yes']
0
['yes']
1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
['yes']
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

Đầu ra

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes

Phương pháp. Sử dụng hiểu danh sách

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
52_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
6

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
55
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
57
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
58
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
61
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
3
['yes']
0
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
64
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
65

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
67

Đầu ra

['yes']

Phương pháp. Sử dụng bool

Một cách tiếp cận là sử dụng hàm bool. Hàm bool trả về Sai cho chuỗi rỗng và True cho chuỗi không trống

Đây là một ví dụ về việc sử dụng hàm bool để kiểm tra xem một chuỗi có rỗng hay không

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
68

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
69_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
6

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
72

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
75
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
76

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
80
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

['yes']
0_______30_______1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
87
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
89

Đầu ra

The string is empty.

Bạn cũng có thể sử dụng hàm bool để kiểm tra xem một chuỗi có trống hay không sau khi xóa mọi khoảng trắng ở đầu hoặc cuối bằng phương thức dải

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
68

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
69_______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
9

 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
94

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7
string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
75
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
98

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
80
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

['yes']
0_______30_______1

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
2
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
87
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
9

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
89

Đầu ra

The string is empty.

Phương pháp. sử dụng dải

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
12

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
13
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
15

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
16
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
18

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
19

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
21

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
25
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

['yes']
0_______10_______28

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
32
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
34

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
16
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
37

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
41
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

['yes']
0_______10_______28

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
48
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

Đầu ra

string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty

Phương pháp. Sử dụng phương thức __eq__

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
50

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
51______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
15

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
54____0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
56

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
19

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
59

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
63
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

['yes']
0_______10_______28

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
70
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
34

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
74

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
78
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

['yes']
0_______10_______28

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
85
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

Đầu ra

string, string1 = '', with no spaces is empty
string, string1 = '   ', with no spaces is not empty

Phương pháp. Sử dụng toán tử “and” + hàm strip()

Python3




The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
12

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
51______0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
15

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
54____0_______5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
18

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
19

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
51
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
51
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
98

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
70
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

['yes']
0_______10_______28

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
63
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
34

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
7
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
54
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
51
['yes']
15

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
['yes']
19
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

['yes']
0_______10_______28

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
24
['yes']
26
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
26

Đầu ra

string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty

Phương pháp “bool” để kiểm tra xem một chuỗi có rỗng hay không có độ phức tạp về thời gian là O(1), vì nó chỉ đơn giản kiểm tra giá trị thực của chuỗi, đây là một phép toán thời gian không đổi. Không gian phụ cũng là O(1) vì nó chỉ yêu cầu một biến boolean duy nhất để lưu trữ giá trị thực của chuỗi