Cho một Chuỗi bao gồm nhiều từ được phân tách bằng dấu cách, hãy viết chương trình Python để lặp lại các từ này của chuỗi
ví dụ
Đầu vào. str = “GeeksforGeeks là một cổng thông tin khoa học máy tính dành cho các Chuyên viên máy tính”
Đầu ra.
GeeksforGeek
là
a
máy tính
khoa học
portal
for
GeeksĐầu vào. str = “Geek for Geek”
Đầu ra.
Geeks
dành cho
Geeks
Phương pháp 1. Sử dụng
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks0
Sử dụng hàm
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks0, chúng ta có thể chia chuỗi thành một danh sách các từ và là phương pháp chung nhất và được đề xuất nếu một người muốn hoàn thành tác vụ cụ thể này. Nhưng nhược điểm là nó bị lỗi trong các trường hợp chuỗi chứa dấu câu
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks2
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks3
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks4
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks5
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks6
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks7
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks0
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks1
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks5
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks3
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks4
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks5
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks6
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks7
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks8
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks5
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks4
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks01
________ 302 ________ 00 ________ 304
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks5
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks06
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks4
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks5
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks09
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks00
________ 301 ________ 302 ________ 303 ________ 304
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks05
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks4
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks07
đầu ra
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks2
Phương pháp 2. Sử dụng
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks08
Trong các trường hợp chứa tất cả các ký tự đặc biệt và dấu chấm câu, như đã thảo luận ở trên, phương pháp tìm từ thông thường trong chuỗi bằng cách sử dụng phân tách có thể không thành công và do đó cần có các biểu thức chính quy để thực hiện tác vụ này. Hàm
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks09 trả về danh sách sau khi lọc chuỗi và trích xuất các từ bỏ qua dấu chấm câu
Trong Python, nhiệm vụ phổ biến là lặp qua một chuỗi để lấy từng ký tự hoặc chia nó thành nhiều đoạn hoặc thậm chí lặp qua chuỗi đó theo thứ tự ngược lại
Làm cách nào để lặp qua một chuỗi trong Python? . Chúng ta cũng có thể sử dụng RANGE với vòng lặp for để lặp qua một loạt các ký tự trong một chuỗi. Một cách khác chúng tôi sử dụng INDEX là trả về từng ký tự của một chỉ mục cụ thể. Chúng ta cũng có thể chia chuỗi thành các đoạn có số lượng ký tự xác định
Bài viết này sẽ giải thích việc lặp qua chuỗi trong Python bằng cách đưa ra nhiều ví dụ thực tế để bạn hiểu rõ hơn
cú pháp
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks3
Tham số đầu vào
- iterator_element. biến sẽ giữ từng giá trị của một chuỗi đã cho
- chuỗi_giá_trị/chuỗi_biến_tên. Các chuỗi mà chúng tôi muốn lặp qua nó
Chuỗi lặp Python Sử dụng vòng lặp for
Ví dụ 1]
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks4
đầu ra
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks5
Trong ví dụ trên, chúng tôi đã tìm nạp phần tử 'python' có giá trị chuỗi và in nó thành các dòng riêng biệt bằng cách sử dụng phép lặp
Chuỗi lặp Python có chỉ mục Sử dụng vòng lặp for và phạm vi []
Ví dụ [2]
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks6
đầu ra
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks7
Trong ví dụ trên, sử dụng vòng lặp for với rang, chúng ta lặp qua phạm vi của chuỗi “mystring” và sau đó in từng giá trị chuỗi trong một dòng riêng biệt
Chuỗi lặp Python có chỉ mục Sử dụng vòng lặp while và phạm vi []
Ví dụ [3]
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks8
đầu ra
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks9
Trong ví dụ trên, sử dụng phép lặp với vòng lặp while, chúng tôi đã tìm nạp phần tử của chuỗi [“Xin chào Oraask”] và in chúng cùng nhau trong các dòng riêng biệt
Python lặp qua các ký tự của một chuỗi
Sử dụng chuỗi [vị trí bắt đầu. vị trí dừng. bước tăng dần], chúng ta có thể lặp qua một phần của biến chuỗi bằng cách tạo một lát chuỗi nhỏ bằng cách sử dụng toán tử chuỗi []
Ví dụ [4]
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks0
đầu ra
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks1
Trong ví dụ trên, bằng cách sử dụng phép lặp với toán tử cắt, chúng tôi đã tìm nạp các phần tử từ vị trí 6 đến vị trí 12 của một chuỗi ["Xin chào Oraask"] là một từ ["Oraask"], sau đó in chúng cùng nhau trong các dòng riêng biệt
Chuỗi lặp Python ngược
Sử dụng chuỗi [vị trí bắt đầu [-]. vị trí dừng [-]. bước tăng dần [-]], chúng ta có thể lặp qua một phần của biến chuỗi ngược lại bằng cách tạo một lát nhỏ của chuỗi bằng cách sử dụng toán tử chuỗi []
Ví dụ [5]
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks2
đầu ra
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks40
Trong ví dụ trên, sử dụng phép lặp với toán tử cắt nhưng có dấu âm cho vị trí bắt đầu, kết thúc và tăng dần, chúng tôi đã tìm nạp các phần tử từ vị trí -1 đến vị trí -7 của một chuỗi [“Xin chào Python”] là một
Python Lặp lại chuỗi 2 ký tự cùng một lúc
Sử dụng chuỗi [vị trí bắt đầu. vị trí dừng. bước tăng dần ], chúng ta có thể lặp qua một biến chuỗi nhưng bỏ qua hai ký tự đồng thời
Ví dụ [6]
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks41
đầu ra
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks42
Trong ví dụ trên, sử dụng vị trí gia tăng để chỉ ra cách trình vòng lặp hoạt động giống như chuỗi lặp hai ký tự cùng một lúc, chúng tôi đã nhảy hai ký tự cùng một lúc qua chuỗi [“HloOas”] bằng cách cưỡi các ký tự [“el rak”]
Chuỗi lặp Python theo khối
Bằng cách sử dụng tính năng Hiểu danh sách với chuỗi, chúng ta có thể chia chuỗi thành các đoạn có độ dài cụ thể rồi trả về kết quả dưới dạng một mảng
Ví dụ [7]
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks43
đầu ra
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks44
Trong ví dụ trên, chúng tôi đã tạo một danh sách mới có tên là các khối để giữ các giá trị mới dưới dạng các khối gồm 4 ký tự, mỗi ký tự trong số các biến “mystring”. Bạn có thể thay đổi giá trị biến “checksplit” để cho biết có bao nhiêu ký tự sẽ được chia thành nhiều phần
Cách lặp qua bảng chữ cái trong Python
Trước tiên, chúng ta có thể lặp qua bảng chữ cái trong python bằng cách sử dụng vòng lặp for đơn giản, chúng ta phải tạo biến bảng chữ cái để có thể lặp qua nó
Ví dụ [8]
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks45
đầu ra
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks46
Giải trình
Trong ví dụ trên, chúng tôi đã nhập mô-đun chuỗi, sau đó, bằng cách sử dụng chuỗi, chúng tôi đã tạo biến alphabetsVar;