Trong bài viết này, chúng tôi muốn hướng dẫn bạn cách tách một chuỗi có nhiều hơn 1 dấu cách giữa các từ trong Python
Ví dụ thực tế
Trong ví dụ này, chúng tôi nhập mô-đun
The original string is : Geeks forGeeks Does string contain spaces ? True5 cho biểu thức chính quy [regex], vì vậy chúng tôi có thể sử dụng phương thức
The original string is : Geeks forGeeks Does string contain spaces ? True6 để tách chuỗi
The original string is : Geeks forGeeks Does string contain spaces ? True7 có hai hoặc nhiều khoảng trắng giữa các từ
Đôi khi, chúng tôi có thể gặp sự cố trong đó chúng tôi cần kiểm tra xem chuỗi có bất kỳ khoảng trống nào không. Loại sự cố này có thể nằm trong miền Machine Learning để lấy loại tập dữ liệu cụ thể. Hãy thảo luận về những cách nhất định mà 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 tiện ích regex do python cung cấp. Bằng cách cung cấp chuỗi regex thích hợp trong tìm kiếm [], chúng tôi có thể kiểm tra sự hiện diện của khoảng trắng trong chuỗi.
Python3
The original string is : Geeks forGeeks Does string contain spaces ? True8
The 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
The original string is : Geeks forGeeks Does string contain spaces ? True5
The original string is : Geeks forGeeks Does string contain spaces ? True3
The original string is : Geeks forGeeks Does string contain spaces ? True0
The original string is : Geeks forGeeks Does string contain spaces ? True1
The original string is : Geeks forGeeks Does string contain spaces ? True2
The 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
The original string is : Geeks forGeeks Does string contain spaces ? True6
The original string is : Geeks forGeeks Does string contain spaces ? True7
The original string is : Geeks forGeeks Does string contain spaces ? True8
The 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 ? True01
________ 102 ________ 01 ________ 104 ________ 105
The original string is : Geeks forGeeks Does string contain spaces ? True06
The original string is : Geeks forGeeks Does string contain spaces ? True4
The original string is : Geeks forGeeks Does string contain spaces ? True08
The original string is : Geeks forGeeks Does string contain spaces ? True8
The original string is : Geeks forGeeks Does string contain spaces ? True00
The original string is : Geeks forGeeks Does string contain spaces ? True01
đầu ra.
The original string is : Geeks forGeeks Does string contain spaces ? True
Phương pháp #2. Sử dụng toán tử in Tác vụ này cũng có thể được thực hiện bằng toán tử in. Chỉ cần kiểm tra khoảng trắng trong chuỗi. Phán quyết được trả về là đúng ngay cả khi tìm thấy một khoảng trắng và sai nếu ngược lại.
Python3
The original string is : Geeks forGeeks Does string contain spaces ? True8
The original string is : Geeks forGeeks Does string contain spaces ? True9
The original string is : Geeks forGeeks Does string contain spaces ? True04
The original string is : Geeks forGeeks Does string contain spaces ? True3
The original string is : Geeks forGeeks Does string contain spaces ? True0
The original string is : Geeks forGeeks Does string contain spaces ? True1
The original string is : Geeks forGeeks Does string contain spaces ? True2
The 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
The original string is : Geeks forGeeks Does string contain spaces ? True6
The original string is : Geeks forGeeks Does string contain spaces ? True7
The original string is : Geeks forGeeks Does string contain spaces ? True8
The original string is : Geeks forGeeks Does string contain spaces ? True9
The original string is : Geeks forGeeks Does string contain spaces ? True04
The original string is : Geeks forGeeks Does string contain spaces ? True01
The original string is : Geeks forGeeks Does string contain spaces ? True02
The original string is : Geeks forGeeks Does string contain spaces ? True1
The original string is : Geeks forGeeks Does string contain spaces ? True90
The original string is : Geeks forGeeks Does string contain spaces ? True91
The original string is : Geeks forGeeks Does string contain spaces ? True0
The original string is : Geeks forGeeks Does string contain spaces ? True06
The original string is : Geeks forGeeks Does string contain spaces ? True4
The original string is : Geeks forGeeks Does string contain spaces ? True08
The original string is : Geeks forGeeks Does string contain spaces ? True8
The original string is : Geeks forGeeks Does string contain spaces ? True00
The original string is : Geeks forGeeks Does string contain spaces ? True01
đầu ra.
The original string is : Geeks forGeeks Does string contain spaces ? True
Phương pháp #3. Sử dụng phương thức find[]
phương thức find[] tìm kiếm chuỗi đã cho để tìm đối số được truyền và trả về vị trí nếu không sẽ trả về -1