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ề 0 cho chuỗi hợp lệ và trống Show
Do đó, kiểm tra với 1 cũng sẽ đánh giá loại 2 và 3 hoặc 4 (chuỗi trống)
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:
>>> 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
Phương thức số 3: Sử dụng phương thức Find ().Python3Tì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. 8 9Output: 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 8 26 27 3 84 The original string is : Geeks forGeeks Does string contain spaces ? True3 Python3 5 6 True2 8 9 5 6 True7 8 9 5 6 False True False2 8False True False4 Output: False True False Ví dụ 2: Ứng dụng thực tếApplicationCho 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:
Python3 5 6 False True False7 False True False8 6Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 80 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 81 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 82 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 83 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 84 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 85 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 86 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 87 6 6 3 80 3 81 3 82 False True False8 3 84 63 86 83 88 5 6 The original string is : Geeks forGeeks Does string contain spaces ? True1 The original string is : Geeks forGeeks Does string contain spaces ? True2 6 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 80 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 81 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 82 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 83 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 84 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 85 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 86 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 87 6 6 3 80 3 81 3 82 False True False8 3 84 63 86 83 88 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. Python3The original string is : Geeks forGeeks Does string contain spaces ? True3 The original string is : Geeks forGeeks Does string contain spaces ? True4 The original string is : Geeks forGeeks Does string contain spaces ? True5 6 The original string is : Geeks forGeeks Does string contain spaces ? True7 8The original string is : Geeks forGeeks Does string contain spaces ? True9 The original string is : Geeks forGeeks Does string contain spaces ? True0 The original string is : Geeks forGeeks Does string contain spaces ? True1 3 84 The original string is : Geeks forGeeks Does string contain spaces ? True3 The original string is : Geeks forGeeks Does string contain spaces ? True4 6 The original string is : Geeks forGeeks Does string contain spaces ? True6 The original string is : Geeks forGeeks Does string contain spaces ? True7 8The original string is : Geeks forGeeks Does string contain spaces ? True9 3 84 01 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. Python3The original string is : Geeks forGeeks Does string contain spaces ? True5 6 The original string is : Geeks forGeeks Does string contain spaces ? True7 8The original string is : Geeks forGeeks Does string contain spaces ? True9 The original string is : Geeks forGeeks Does string contain spaces ? True0 The original string is : Geeks forGeeks Does string contain spaces ? True1 3 84 The original string is : Geeks forGeeks Does string contain spaces ? True3 The original string is : Geeks forGeeks Does string contain spaces ? True4 6 The original string is : Geeks forGeeks Does string contain spaces ? True6 The original string is : Geeks forGeeks Does string contain spaces ? True7 8The original string is : Geeks forGeeks Does string contain spaces ? True9 3 84 01 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 ? True4 6 14Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 83 16Python3Phươ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. 30 6 0Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 86 34 35 36 6 383 86 40Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 85 30 63 80 8 26 47 3 84 01 02Đầu ra The original string is : Geeks forGeeks Does string contain spaces ? True
Python3Phươ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. 59 6Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 80 30 6 0Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 81 66Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 83 68Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 85 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 86 713 82 593 84 63 86 Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 86 78 63 86 40Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 85 30 63 80 8 26 47 3 84 01 02Đầu ra The original string is : Geeks forGeeks Does string contain spaces ? True |