Hướng dẫn how do you split a statement in python? - làm thế nào để bạn tách một câu lệnh trong python?

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức phân chia chuỗi python () với sự trợ giúp của các ví dụ.

Phương thức split() phá vỡ một chuỗi ở dấu phân cách được chỉ định và trả về một danh sách các chuỗi.

Thí dụ

text = 'Python is a fun programming language'

# split the text from space print(text.split(' '))

# Output: ['Python', 'is', 'a', 'fun', 'programming', 'language']

Cú pháp của Chuỗi chia ()

Cú pháp của split() là:

str.split(separator, maxsplit)

Chia () tham số

Phương thức split() mất tối đa 2 tham số:

  • phân tách (tùy chọn)- DELIMITER tại đó xảy ra sự chia tách. Nếu không được cung cấp, chuỗi được chia tại khoảng trắng. (optional)- Delimiter at which splits occur. If not provided, the string is splitted at whitespaces.
  • MaxSplit (Tùy chọn) - Số lượng phân tách tối đa. Nếu không được cung cấp, không có giới hạn về số lượng chia tách. (optional) - Maximum number of splits. If not provided, there is no limit on the number of splits.

chia () giá trị trả về

Phương thức split() trả về một danh sách các chuỗi.


Ví dụ 1: Làm thế nào chia () hoạt động trong Python?

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))

Đầu ra

['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']

Ví dụ 2: Làm thế nào chia () hoạt động khi MaxSplit được chỉ định?

grocery = 'Milk, Chicken, Bread, Butter'

# maxsplit: 2

print(grocery.split(', ', 2))

# maxsplit: 1 print(grocery.split(', ', 1)) # maxsplit: 5

print(grocery.split(', ', 5))

# maxsplit: 0 print(grocery.split(', ', 0))

Đầu ra

['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']

Ví dụ 2: Làm thế nào chia () hoạt động khi MaxSplit được chỉ định?

Phương thức phân chia chuỗi python trong Python chia một chuỗi thành một danh sách các chuỗi sau khi phá chuỗi đã cho bằng cách phân cách được chỉ định. in Python split a string into a list of strings after breaking the given string by the specified separator.

Phương thức phân chia chuỗi Python

Cú pháp: str.split (phân tách, MaxSplit) str.split(separator, maxsplit)

Thông số :

  • DEVERATOR: Đây là một dấu phân cách. Chuỗi chia tách tại phân tách được chỉ định này. Nếu không được cung cấp thì bất kỳ không gian trắng là một dấu tách. This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
  • MAXSplit: Đó là một số, cho chúng ta biết chia chuỗi thành tối đa số lần được cung cấp. Nếu nó không được cung cấp thì mặc định là -1 có nghĩa là không có giới hạn. It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then the default is -1 that means there is no limit.

Trả về: Trả về một danh sách các chuỗi sau khi phá chuỗi đã cho bởi bộ phân cách được chỉ định. Returns a list of strings after breaking the given string by the specified separator.

Ví dụ Phương thức phân chia chuỗi Python

Python3

str.split(separator, maxsplit)
4
str.split(separator, maxsplit)
5
str.split(separator, maxsplit)
6

str.split(separator, maxsplit)
7
str.split(separator, maxsplit)
5
str.split(separator, maxsplit)
9
text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
0
text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
1

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
2
text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
3

Output:

['one', 'two', 'three']

Ví dụ 1: Ví dụ để chứng minh chức năng Split () hoạt động như thế nào Example to demonstrate how split() function works

Ở đây chúng tôi đang sử dụng chức năng phân chia chuỗi python () để chia các chuỗi khác nhau thành một danh sách, được phân tách bởi các ký tự khác nhau trong mỗi trường hợp.

Python3

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
4
str.split(separator, maxsplit)
5
text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
6

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
2
text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
8

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
9
str.split(separator, maxsplit)
5
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
1

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
2
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
3
text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
0
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
5

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
9
str.split(separator, maxsplit)
5
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
8

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
2
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
3
grocery = 'Milk, Chicken, Bread, Butter'

# maxsplit: 2

print(grocery.split(', ', 2))

# maxsplit: 1 print(grocery.split(', ', 1)) # maxsplit: 5

print(grocery.split(', ', 5))

# maxsplit: 0 print(grocery.split(', ', 0))
1
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
5

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
9
str.split(separator, maxsplit)
5
grocery = 'Milk, Chicken, Bread, Butter'

# maxsplit: 2

print(grocery.split(', ', 2))

# maxsplit: 1 print(grocery.split(', ', 1)) # maxsplit: 5

print(grocery.split(', ', 5))

# maxsplit: 0 print(grocery.split(', ', 0))
5

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
2
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
3
grocery = 'Milk, Chicken, Bread, Butter'

# maxsplit: 2

print(grocery.split(', ', 2))

# maxsplit: 1 print(grocery.split(', ', 1)) # maxsplit: 5

print(grocery.split(', ', 5))

# maxsplit: 0 print(grocery.split(', ', 0))
8
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
5

Đầu ra:

['geeks', 'for', 'geeks']
['geeks', ' for', ' geeks']
['geeks', 'for', 'geeks']
['Ca', 'Ba', 'Sa', 'Fa', 'Or']

Ví dụ 2: Ví dụ để chứng minh chức năng phân tách () hoạt động như thế nào khi MaxSplit được chỉ định Example to demonstrate how split() function works when maxsplit is specified

Tham số MaxSplit được sử dụng để kiểm soát số lượng phân tách để trả về sau khi chuỗi được phân tích cú pháp. Ngay cả khi có nhiều lần phân tách có thể, nó sẽ chỉ thực hiện tối đa số lượng phân tách theo định nghĩa của tham số MaxSplit.

Python3

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
9
str.split(separator, maxsplit)
5
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']
2

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
2
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
3
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']
5
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']
6
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']
7
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
5

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
2
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
3
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']
5
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']
6
['one', 'two', 'three']
3
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
5

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))
2
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
3
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']
5
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']
6
['one', 'two', 'three']
9
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
5

Đầu ra:

['geeks, for, geeks, pawan']
['geeks', 'for', 'geeks', 'pawan']
['geeks', 'for, geeks, pawan']

Có một chức năng phân chia trong Python?

Hàm thao tác chuỗi trong python được sử dụng để chia một chuỗi lớn hơn thành một số chuỗi nhỏ hơn được gọi là hàm chia () trong python. Hàm chia () trả về các chuỗi dưới dạng danh sách.. The split() function returns the strings as a list.

Làm thế nào để bạn chia đầu ra trong Python?

Python Chuỗi phân chia () Phân tách cú pháp Phương thức: Đây là một dấu phân cách.Chuỗi chia tách tại phân tách được chỉ định này.Nếu không được cung cấp thì bất kỳ không gian trắng là một dấu tách.MAXSplit: Đó là một số, cho chúng ta biết chia chuỗi thành tối đa số lần được cung cấp.

Bạn có thể chia tay hai thứ bằng Python không?

Phương pháp 1: Chia nhiều ký tự từ chuỗi bằng cách sử dụng re.split () Đây là phương pháp hiệu quả nhất và thường được sử dụng để phân chia nhiều ký tự cùng một lúc.Nó sử dụng regex (biểu thức thông thường) để làm điều này.Split multiple characters from string using re. split() This is the most efficient and commonly used method to split multiple characters at once. It makes use of regex(regular expressions) in order to do this.

Làm thế nào để bạn chia hai biến trong Python?

Để phân chia các biến chuỗi ở mỗi khoảng trắng, chúng ta có thể sử dụng hàm chia () mà không có đối số.Hàm cú pháp cho split () được chia (phân tách, maxsplit) trong đó bộ phân cách chỉ định ký tự tại đó chuỗi nên được phân tách.MaxSplit Chỉ định số lần chuỗi phải được phân chia.