Hướng dẫn python cut string after number of characters - python cắt chuỗi sau số ký tự

Đã nâng cấp hai câu trả lời khác, nhưng chỉ vì niềm vui, với Regex:

import re

r = re.compile('.{,20}(?

kết quả

-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''

Xóa mọi thứ sau một ký tự trong một chuỗi trong Python #

Để xóa mọi thứ sau một ký tự trong một chuỗi:

  1. Sử dụng phương thức
    -----------------------
     ....5...10...15...20
    'This is a sample text'
    'This is a sample'
    -----------------------
     ....5...10...15...20
    'abcdefghijklmnopqrstuvwxyz  '
    ''
    -----------------------
     ....5...10...15...20
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ''
    -----------------------
     ....5...10...15...20
    'This is 1 first sample text  '
    'This is 1 first'
    -----------------------
     ....5...10...15...20
    'This is 1 again sample text'
    'This is 1 again'
    -----------------------
     ....5...10...15...20
    'A great blank          here'
    'A great blank'
    -----------------------
     ....5...10...15...20
    'Another blank     here'
    'Another blank'
    -----------------------
     ....5...10...15...20
    'A short text'
    'A short text'
    -----------------------
     ....5...10...15...20
    '  little indent'
    '  little indent'
    -----------------------
     ....5...10...15...20
    '                      great indent'
    ''
    -----------------------
     ....5...10...15...20
    'ocean'
    'ocean'
    -----------------------
     ....5...10...15...20
    '!'
    '!'
    -----------------------
     ....5...10...15...20
    ''
    ''
    
    0 để phân chia chuỗi trên dấu phân cách.
  2. Truy cập phần tử danh sách tại Index
    -----------------------
     ....5...10...15...20
    'This is a sample text'
    'This is a sample'
    -----------------------
     ....5...10...15...20
    'abcdefghijklmnopqrstuvwxyz  '
    ''
    -----------------------
     ....5...10...15...20
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ''
    -----------------------
     ....5...10...15...20
    'This is 1 first sample text  '
    'This is 1 first'
    -----------------------
     ....5...10...15...20
    'This is 1 again sample text'
    'This is 1 again'
    -----------------------
     ....5...10...15...20
    'A great blank          here'
    'A great blank'
    -----------------------
     ....5...10...15...20
    'Another blank     here'
    'Another blank'
    -----------------------
     ....5...10...15...20
    'A short text'
    'A short text'
    -----------------------
     ....5...10...15...20
    '  little indent'
    '  little indent'
    -----------------------
     ....5...10...15...20
    '                      great indent'
    ''
    -----------------------
     ....5...10...15...20
    'ocean'
    'ocean'
    -----------------------
     ....5...10...15...20
    '!'
    '!'
    -----------------------
     ....5...10...15...20
    ''
    ''
    
    1 để có được mọi thứ trước dấu phân cách.
  3. Tùy chọn, sử dụng toán tử bổ sung (+) để thêm dấu phân cách.

Copied!

my_str = 'one!two!three' # ✅ remove everything after FIRST occurrence of character separator = '!' result_1 = my_str.split(separator, 1)[0] print(result_1) # 👉️ 'one' # --------------------------------------------------------- # ✅ remove everything after LAST occurrence of character result_2 = my_str.rsplit(separator, 1)[0] print(result_2) # 👉️ 'one!two' # --------------------------------------------------------- # ✅ remove everything after character, keeping separator result_3 = my_str.split(separator, 1)[0] + separator print(result_3) # 👉️ 'one!'

Chúng tôi đã sử dụng phương thức

-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
0 để loại bỏ mọi thứ sau một ký tự (
-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
3 trong các ví dụ).

Phương thức str.split () chia chuỗi thành một danh sách các chuỗi con bằng cách sử dụng dấu phân cách.

Phương thức lấy 2 tham số sau:

TênSự mô tả
máy tách biệtChia chuỗi thành chuỗi con trên mỗi lần xuất hiện
MaxSplitNhiều nhất
-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
4 chia tách được thực hiện (tùy chọn)

Nếu bộ phân cách không được tìm thấy trong chuỗi, một danh sách chỉ chứa 1 phần tử được trả về.

Chúng tôi đặt đối số

-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
4 thành
-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
6 vì chúng tôi chỉ cần chia chuỗi một lần.

Ví dụ đầu tiên loại bỏ mọi thứ sau lần xuất hiện đầu tiên của ký tự trong chuỗi.

Copied!

my_str = 'one!two!three' # ✅ remove everything after FIRST occurrence of character separator = '!' result_1 = my_str.split(separator, 1)[0] print(result_1) # 👉️ 'one' # 👇️ ['one', 'two!three'] print(my_str.split(separator, 1))

Lưu ý rằng bộ phân cách không được bao gồm trong chuỗi. Nếu bạn cần bao gồm nó, hãy sử dụng toán tử bổ sung (+).

Copied!

my_str = 'one!two!three' # ✅ remove everything after character, keeping separator separator = '!' result_1 = my_str.split(separator, 1)[0] + separator print(result_1) # 👉️ 'one!'

Toán tử bổ sung (+) có thể được sử dụng để nối các chuỗi trong Python.

Nếu bạn cần xóa mọi thứ sau lần xuất hiện cuối cùng của ký tự trong chuỗi, hãy sử dụng phương thức

-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
7.

Copied!

my_str = 'one!two!three' separator = '!' # ✅ remove everything after LAST occurrence of character result_2 = my_str.rsplit(separator, 1)[0] print(result_2) # 👉️ 'one!two'

Ngoại trừ việc chia tách từ bên phải,

-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
8 hoạt động như
-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
9.

Phương thức

-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
7 chia chuỗi từ bên phải và với
-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
4 được đặt thành
-----------------------
 ....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
 ....5...10...15...20
'abcdefghijklmnopqrstuvwxyz  '
''
-----------------------
 ....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
 ....5...10...15...20
'This is 1 first sample text  '
'This is 1 first'
-----------------------
 ....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
 ....5...10...15...20
'A great blank          here'
'A great blank'
-----------------------
 ....5...10...15...20
'Another blank     here'
'Another blank'
-----------------------
 ....5...10...15...20
'A short text'
'A short text'
-----------------------
 ....5...10...15...20
'  little indent'
'  little indent'
-----------------------
 ....5...10...15...20
'                      great indent'
''
-----------------------
 ....5...10...15...20
'ocean'
'ocean'
-----------------------
 ....5...10...15...20
'!'
'!'
-----------------------
 ....5...10...15...20
''
''
6, nó chỉ chia tách một lần.

Nếu bạn cần bao gồm ký tự bạn chia tay, hãy sử dụng toán tử bổ sung (+).

Copied!

my_str = 'one!two!three' separator = '!' result_2 = my_str.rsplit(separator, 1)[0] + separator print(result_2) # 👉️ 'one!two!'

Bạn cũng có thể sử dụng phương thức

Copied!

my_str = 'one!two!three' # ✅ remove everything after FIRST occurrence of character separator = '!' result_1 = my_str.split(separator, 1)[0] print(result_1) # 👉️ 'one' # --------------------------------------------------------- # ✅ remove everything after LAST occurrence of character result_2 = my_str.rsplit(separator, 1)[0] print(result_2) # 👉️ 'one!two' # --------------------------------------------------------- # ✅ remove everything after character, keeping separator result_3 = my_str.split(separator, 1)[0] + separator print(result_3) # 👉️ 'one!'
3 để xóa mọi thứ sau một ký tự cụ thể trong một chuỗi.

Copied!

my_str = 'one!two!three' separator = '!' result_1 = my_str.partition(separator)[0] print(result_1) # 👉️ 'one' result_2 = ''.join(my_str.partition(separator)[0:2]) print(result_2) # 👉️ 'one!'

Phương thức str.partition phân tách chuỗi ở lần xuất hiện đầu tiên của bộ phân cách được cung cấp.

Phương thức trả về một tuple chứa 3 phần tử - phần trước dấu phân cách, bộ phân cách và phần sau khi phân tách.

Copied!

my_str = 'one!two!three' separator = '!' # 👇️ ('one', '!', 'two!three') print(my_str.partition(separator))

Nếu bộ phân tách không được tìm thấy trong chuỗi, phương thức trả về một tuple chứa chuỗi, theo sau là 2 chuỗi trống.

Nếu bạn cần đưa bộ phân cách vào kết quả, hãy sử dụng phương thức

Copied!

my_str = 'one!two!three' # ✅ remove everything after FIRST occurrence of character separator = '!' result_1 = my_str.split(separator, 1)[0] print(result_1) # 👉️ 'one' # --------------------------------------------------------- # ✅ remove everything after LAST occurrence of character result_2 = my_str.rsplit(separator, 1)[0] print(result_2) # 👉️ 'one!two' # --------------------------------------------------------- # ✅ remove everything after character, keeping separator result_3 = my_str.split(separator, 1)[0] + separator print(result_3) # 👉️ 'one!'
4 để tham gia các mục danh sách thứ nhất và thứ hai.

Copied!

my_str = 'one!two!three' separator = '!' result_2 = ''.join(my_str.partition(separator)[0:2]) print(result_2) # 👉️ 'one!'

Phương thức str.join lấy một điều đáng tin cậy như một đối số và trả về một chuỗi là sự kết hợp của các chuỗi trong điều kiện có thể sử dụng được.

Chuỗi phương thức được gọi là bật được sử dụng làm phân tách giữa các phần tử.

Làm thế nào để bạn chia một chuỗi sau một số nhân vật nhất định Python?

Phương thức python split () được sử dụng để chia chuỗi thành các khối và nó chấp nhận một đối số gọi là phân tách. Một dấu phân cách có thể là bất kỳ ký tự hoặc một biểu tượng. Nếu không có dấu phân cách nào được xác định, thì nó sẽ phân chia chuỗi đã cho và khoảng trắng sẽ được sử dụng theo mặc định. is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.

Làm cách nào để cắt một chuỗi sau một nhân vật trong Python?

Để loại bỏ mọi thứ sau một ký tự trong một chuỗi:..
Sử dụng str.phương thức chia () để chia chuỗi trên dấu phân cách ..
Truy cập phần tử danh sách tại Index 0 để nhận mọi thứ trước khi phân tách ..
Tùy chọn, sử dụng toán tử bổ sung (+) để thêm dấu phân cách ..

Làm thế nào để bạn cắt một chuỗi sau một nhân vật nhất định?

Để cắt một chuỗi sau một ký tự cụ thể, bạn có thể sử dụng phương thức chuỗi con (), phương thức cắt () hoặc phương thức chia ().Các phương thức lát () và các phương thức chuỗi con () hoạt động giống như chúng trích xuất chuỗi bằng cách cắt các phần khác dựa trên ký tự cụ thể.use the substring() method, slice() method, or split() method. The slice() and the substring() methods work the same as they extract the string by cutting other parts based on the specific character.

Làm cách nào để xóa tất cả các ký tự khỏi một chuỗi sau một ký tự cụ thể?

Để xóa mọi thứ sau một ký tự cụ thể trong một chuỗi: sử dụng phương thức chuỗi.split () để phân chia chuỗi trên ký tự.Truy cập mảng tại chỉ mục 0.Phần tử đầu tiên trong mảng sẽ là một phần của chuỗi trước ký tự được chỉ định.Use the String. split() method to split the string on the character. Access the array at index 0 . The first element in the array will be the part of the string before the specified character.