Hướng dẫn how to split a string at every character in python - cách tách một chuỗi ở mỗi ký tự trong python

Đưa ra một chuỗi, hãy viết một chương trình Python để chia các ký tự của chuỗi đã cho thành một danh sách bằng Python.

Examples:    

Input : geeks
Output : ['g', 'e', 'e', 'k', 's']

Input : Word
Output : ['W', 'o', 'r', 'd']

Phương pháp 1: Chia một chuỗi thành danh sách Python bằng phương thức unpack (*)

Hành động giải nén liên quan đến việc loại bỏ mọi thứ, cụ thể là các chữ viết như từ điển, danh sách và bộ dữ liệu.

Python3

['g', 'e', 'e', 'k', 's']
2
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's']
4

['g', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's']
6
['g', 'e', 'e', 'k', 's']
7
['g', 'e', 'e', 'k', 's']
8

Output:  

['g', 'e', 'e', 'k', 's']

Phương pháp 2: Chia một chuỗi thành danh sách Python bằng cách sử dụng vòng lặp

Ở đây, chúng tôi đang chia các chữ cái bằng cách sử dụng bản địa bằng cách sử dụng vòng lặp và sau đó chúng tôi đang nối nó vào một danh sách mới.

Python3

['g', 'e', 'e', 'k', 's']
2
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
1

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
2
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
4

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
6
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
7
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
8

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
9
['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
0

['g', 'e', 'e', 'k', 's']
5
['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
2

Output:

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']

Phương pháp 3: Chia một chuỗi thành danh sách python bằng cách sử dụng danh sách hiểu

Cách tiếp cận này sử dụng danh sách hiểu để chuyển đổi từng ký tự thành một danh sách. Sử dụng cú pháp sau, bạn có thể chia các ký tự của chuỗi thành danh sách.

Python3

['g', 'e', 'e', 'k', 's']
2
['g', 'e', 'e', 'k', 's']
3
['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
5

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
6
['g', 'e', 'e', 'k', 's']
3
['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
8
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's']
0
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
7
['g', 'e', 'e', 'k', 's']
2

['g', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's']
4

Output:  

['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']

Phương pháp 4: Chia một chuỗi thành danh sách Python bằng Danh sách () & NBSP; Typecasting

Python cung cấp đánh máy trực tiếp các chuỗi vào danh sách bằng danh sách Python ().

Python3

['g', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's']
6

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
9
['g', 'e', 'e', 'k', 's']
8
['g', 'e', 'e', 'k', 's']
9
['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']
0

['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']
1
['g', 'e', 'e', 'k', 's']
3
['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']
3

['g', 'e', 'e', 'k', 's']
5
['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']
5

Output:  

['g', 'e', 'e', 'k', 's']

Phương pháp 5: Chia một chuỗi thành danh sách Python bằng cách sử dụng Extend ()

Mở rộng lặp lại qua đầu vào của nó, mở rộng danh sách và thêm từng thành viên.

Python3

['g', 'e', 'e', 'k', 's']
2
['g', 'e', 'e', 'k', 's']
3
['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']
8

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
2
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
4

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2

['g', 'e', 'e', 'k', 's']
5
['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
2

Output:  

['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']

Đôi khi chúng ta cần làm việc chỉ với các danh sách và do đó chuỗi có thể cần được chuyển đổi thành danh sách. Nó phải được chuyển đổi trong danh sách các ký tự cho một số nhiệm vụ nhất định được thực hiện. Điều này thường được yêu cầu trong việc học máy để xử lý dữ liệu và phân loại văn bản. Hãy để thảo luận về những cách nhất định trong đó nhiệm vụ này được thực hiện.

Phương pháp số 1: Sử dụng Danh sách cắt

Cắt danh sách có thể được sử dụng cho mục đích cụ thể này, trong đó chúng tôi gán cho từng phần tử chỉ mục của danh sách ký tự xảy ra tiếp theo của chuỗi bằng cách sử dụng thao tác lát cắt. & NBSP;

Python3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's']
3
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
7

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
0
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
4
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
4

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
7
['g', 'e', 'e', 'k', 's']
3
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5

Đầu ra

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']

Phương pháp số 2: Sử dụng Danh sách () & NBSP; 

Cách ngắn gọn và có thể đọc được nhất để thực hiện phân tách là nhập chuỗi trường hợp vào danh sách và việc chia danh sách được tự động xử lý nội bộ. Đây là phương thức được khuyến nghị để thực hiện nhiệm vụ cụ thể này. & NBSP;

Python3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's']
3
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
7

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
0
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
4
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
4

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5

Đầu ra

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']

Phương pháp số 2: Sử dụng Danh sách () & NBSP;

Cách ngắn gọn và có thể đọc được nhất để thực hiện phân tách là nhập chuỗi trường hợp vào danh sách và việc chia danh sách được tự động xử lý nội bộ. Đây là phương thức được khuyến nghị để thực hiện nhiệm vụ cụ thể này. & NBSP;

Python3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's']
3
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
7

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
0
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
4
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
4

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
7
['g', 'e', 'e', 'k', 's']
3
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9

Đầu ra

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']

Phương pháp số 2: Sử dụng Danh sách () & NBSP;

Cách ngắn gọn và có thể đọc được nhất để thực hiện phân tách là nhập chuỗi trường hợp vào danh sách và việc chia danh sách được tự động xử lý nội bộ. Đây là phương thức được khuyến nghị để thực hiện nhiệm vụ cụ thể này. & NBSP;

Python3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's']
3
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
7

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
0
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
4
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
4

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
7
['g', 'e', 'e', 'k', 's']
3
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5

Đầu ra

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']

Phương pháp số 2: Sử dụng Danh sách () & NBSP; Using re.findall() method: We can use above mention method to find all character in the string and form a list of characters.

Cách ngắn gọn và có thể đọc được nhất để thực hiện phân tách là nhập chuỗi trường hợp vào danh sách và việc chia danh sách được tự động xử lý nội bộ. Đây là phương thức được khuyến nghị để thực hiện nhiệm vụ cụ thể này. & NBSP;

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
4
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
8

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5
['g', 'e', 'e', 'k', 's']
3
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
7

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
0
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
4
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
4

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5

Đầu ra

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']

Phương pháp số 2: Sử dụng Danh sách () & NBSP;

Python3

Cách ngắn gọn và có thể đọc được nhất để thực hiện phân tách là nhập chuỗi trường hợp vào danh sách và việc chia danh sách được tự động xử lý nội bộ. Đây là phương thức được khuyến nghị để thực hiện nhiệm vụ cụ thể này. & NBSP;

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
0
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
3

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
4
['g', 'e', 'e', 'k', 's']
3
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
4

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
7
['g', 'e', 'e', 'k', 's']
3
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
9
['g', 'e', 'e', 'k', 's']
91

['g', 'e', 'e', 'k', 's']
5
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
9
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
1
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
2
The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
5

Đầu ra

['g', 'e', 'e', 'k', 's']
1


Làm thế nào để chia các ký tự của một chuỗi thành một danh sách trong Python?

Đưa ra một chuỗi, hãy viết một chương trình Python để chia các ký tự của chuỗi đã cho thành một danh sách. Cách tiếp cận này sử dụng danh sách hiểu để chuyển đổi từng ký tự thành một danh sách. Sử dụng cú pháp sau, bạn có thể chia các ký tự của chuỗi thành danh sách. Python cung cấp kiểu chữ trực tiếp của chuỗi vào danh sách bằng danh sách (). Geek chú ý!

Hàm chia () trong Python là gì?

Để phân tách chuỗi python cung cấp một hàm gọi là split ().Hàm chia () là gì?Phương thức Split () phá vỡ một chuỗi đã cho bằng một dấu phân cách được chỉ định và trả về một danh sách các chuỗi.

Làm thế nào để chia một chuỗi sau mỗi n ký tự?

Tóm tắt: Một trong những cách dễ nhất để phân chia chuỗi sau mỗi ký tự N là sử dụng danh sách hiểu và cắt chuỗi phù hợp để trích xuất mọi ký tự N của chuỗi đã cho và lưu trữ chúng trong danh sách.Các mục của danh sách này đại diện cho các chuỗi con phân chia cần thiết.

Làm thế nào để bạn chia một chuỗi với các giá trị khác nhau?

Bằng cách chuyển các giá trị khác nhau cho hàm chia (), chúng ta có thể chia một chuỗi theo nhiều cách khác nhau.Chúng ta có thể chỉ định ký tự để phân chia một chuỗi bằng cách sử dụng bộ phân cách trong hàm chia ().Theo mặc định, Split () sẽ sử dụng khoảng trắng làm dấu phân cách, nhưng chúng tôi có thể tự do cung cấp các ký tự khác nếu chúng tôi muốn.