Chuỗi Python là bất biến và do đó có khả năng xử lý phức tạp hơn khi nói về các hoạt động của nó. Lưu ý rằng một chuỗi có khoảng trắng thực sự là một chuỗi trống nhưng có kích thước không khác. Bài viết này cũng đã thảo luận về vấn đề và giải pháp cho nó. & NBSP; Hãy để xem các phương pháp khác nhau để kiểm tra xem chuỗi có trống hay không.
Let’s see different methods of checking if string is empty or not.
Phương pháp số 1: Sử dụng Len [] & NBSP;
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 nên được coi là chuỗi trống ngay cả không có số 0 của nó.
Python3
test_str1
=
""
test_str2
=
" "
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No0
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No2
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No3
=
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No5
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No9
=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes4
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No0
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes7
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes9
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes0
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes4
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No0
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes4
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes8
____10
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No3
=
__The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
test_str1
5=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes4
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No0
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes7
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes9
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes0
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes4
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No0
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes4
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes8
Đầu ra: & nbsp; & nbsp;
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No
Phương pháp số 2: Sử dụng không & nbsp;
Không thể vận hành cũng có thể thực hiện tác vụ tương tự như Len [] và kiểm tra chuỗi độ dài 0, nhưng giống như ở trên, nó coi chuỗi chỉ với khoảng trống cũng không phải là trống, thực tế không phải là đúng. & NBSP; & NBSP ;
Python3
test_str1
=
""
test_str2
=
" "
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No9
=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
____10
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No3
=
__The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
test_str1
5=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
Đầu ra: & nbsp; & nbsp;
Phương pháp số 2: Sử dụng không & nbsp;
Không thể vận hành cũng có thể thực hiện tác vụ tương tự như Len [] và kiểm tra chuỗi độ dài 0, nhưng giống như ở trên, nó coi chuỗi chỉ với khoảng trống cũng không phải là trống, thực tế không phải là đúng. & NBSP; & NBSP ;
Is
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
test_str1
5=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
Đầu ra: & nbsp; & nbsp;
Phương pháp số 2: Sử dụng không & nbsp;
Đầu ra: & nbsp; & nbsp;
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No
Phương pháp số 2: Sử dụng không & nbsp;
Không thể vận hành cũng có thể thực hiện tác vụ tương tự như Len [] và kiểm tra chuỗi độ dài 0, nhưng giống như ở trên, nó coi chuỗi chỉ với khoảng trống cũng không phải là trống, thực tế không phải là đúng. & NBSP; & NBSP ;
Is
test_str1
=
""
test_str2
=
" "
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No9
=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
____10
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No3
=
__The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
test_str1
5=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
Đầu ra: & nbsp; & nbsp;
Phương pháp số 2: Sử dụng không & nbsp;
Không thể vận hành cũng có thể thực hiện tác vụ tương tự như Len [] và kiểm tra chuỗi độ dài 0, nhưng giống như ở trên, nó coi chuỗi chỉ với khoảng trống cũng không phải là trống, thực tế không phải là đúng. & NBSP; & NBSP ;
Is
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
test_str1
5=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
Đầu ra: & nbsp; & nbsp;
Phương pháp số 2: Sử dụng không & nbsp;
Output:
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes
Không thể vận hành cũng có thể thực hiện tác vụ tương tự như Len [] và kiểm tra chuỗi độ dài 0, nhưng giống như ở trên, nó coi chuỗi chỉ với khoảng trống cũng không phải là trống, thực tế không phải là đúng. & NBSP; & NBSP ;
Is
Python3
test_str1
=
""
test_str2
=
" "
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No9
=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
____10
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes8
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No3
=
__The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
test_str1
5=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
Đầu ra: & nbsp; & nbsp;
Phương pháp số 2: Sử dụng không & nbsp;
Không thể vận hành cũng có thể thực hiện tác vụ tương tự như Len [] và kiểm tra chuỗi độ dài 0, nhưng giống như ở trên, nó coi chuỗi chỉ với khoảng trống cũng không phải là trống, thực tế không phải là đúng. & NBSP; & NBSP ;
Is
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No6
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No1
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No8
test_str1
5=
__ The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes222
Đầu ra: & nbsp; & nbsp;
Phương pháp số 2: Sử dụng không & nbsp;
Đầu ra: & nbsp; & nbsp;
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes
Phương pháp số 2: Sử dụng không & nbsp;
Python3
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes22
=
""
Không thể vận hành cũng có thể thực hiện tác vụ tương tự như Len [] và kiểm tra chuỗi độ dài 0, nhưng giống như ở trên, nó coi chuỗi chỉ với khoảng trống cũng không phải là trống, thực tế không phải là đúng. & NBSP; & NBSP ;
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No0
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes37