Nhân đôi không gian trong Python

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 ? True
5 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 ? True
6 để tách chuỗi
The original string is : Geeks  forGeeks
Does string contain spaces ? True
7 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 ? 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
The original string is : Geeks  forGeeks
Does string contain spaces ? True
5

 

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

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

 

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
The original string is : Geeks  forGeeks
Does string contain spaces ? True
6
The original string is : Geeks  forGeeks
Does string contain spaces ? True
7
The original string is : Geeks  forGeeks
Does string contain spaces ? 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
01

________ 102 ________ 01 ________ 104 ________ 105

 

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

The original string is : Geeks  forGeeks
Does string contain spaces ? True
4
The original string is : Geeks  forGeeks
Does string contain spaces ? True
08
The original string is : Geeks  forGeeks
Does string contain spaces ? True
8
The original string is : Geeks  forGeeks
Does string contain spaces ? True
00
The original string is : Geeks  forGeeks
Does string contain spaces ? True
01

đầ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 ? 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
04

 

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

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

 

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
The original string is : Geeks  forGeeks
Does string contain spaces ? True
6
The original string is : Geeks  forGeeks
Does string contain spaces ? True
7
The original string is : Geeks  forGeeks
Does string contain spaces ? 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
04

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

The original string is : Geeks  forGeeks
Does string contain spaces ? True
02
The original string is : Geeks  forGeeks
Does string contain spaces ? True
1
The original string is : Geeks  forGeeks
Does string contain spaces ? True
90
The original string is : Geeks  forGeeks
Does string contain spaces ? True
91
The original string is : Geeks  forGeeks
Does string contain spaces ? True
0

 

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

The original string is : Geeks  forGeeks
Does string contain spaces ? True
4
The original string is : Geeks  forGeeks
Does string contain spaces ? True
08
The original string is : Geeks  forGeeks
Does string contain spaces ? True
8
The original string is : Geeks  forGeeks
Does string contain spaces ? True
00
The original string is : Geeks  forGeeks
Does string contain spaces ? True
01

đầ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

\n có thêm khoảng trắng không?

Python \n cung cấp hai khoảng trắng ở giữa đầu ra và \t cung cấp một.

Khoảng cách trong Python là gì?

Trong Python, các ký tự dùng để giãn cách được gọi là ký tự khoảng trắng . Chúng bao gồm dòng mới, dấu cách, tab, xuống dòng, nguồn cấp dữ liệu, v.v. Phương thức isspace[] của Chuỗi Python được sử dụng để xác định xem một đối số có tất cả các ký tự khoảng trắng chẳng hạn như. ' ' - Khoảng trống. '\t' – Tab ngang.

Nó có nghĩa là gì không gian đôi?

Nếu bạn đánh dấu cách đôi nội dung mà bạn đang viết hoặc nhập, bạn bao gồm một dòng đầy đủ giữa mỗi dòng viết .

Chủ Đề