Làm thế nào để bạn chia một chuỗi thành hai trong python?

Giải nén các giá trị để chia một chuỗi thành nhiều biến, e. g.

string.split(separator, maxsplit)
9. Phương thức
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

0 sẽ chia chuỗi thành một danh sách các chuỗi, danh sách này có thể được gán cho các biến trong một lần khai báo

Show

Ví dụ này chia chuỗi thành một danh sách các chuỗi trên mỗi khoảng trắng, nhưng bạn có thể sử dụng bất kỳ dấu phân cách nào khác

Đảm bảo khai báo chính xác số lượng biến có trong danh sách

Ví dụ: nếu bạn chỉ muốn 2 mục đầu tiên trong danh sách, bạn có thể sử dụng đối số

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

1 khi gọi hàm
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

0

các str. split() chia chuỗi thành 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 nhận 2 tham số sau

NameDescriptionseparatorChia chuỗi thành các chuỗi con trên mỗi lần xuất hiện của dấu phân cáchmaxsplit Tối đa 31 lần phân tách được thực hiện (tùy chọn)

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

Nếu bạn cố giải nén nhiều hơn hoặc ít giá trị hơn giá trị có trong danh sách, bạn sẽ gặp lỗi

Ta khai báo 2 biến nhưng danh sách chứa 3 biến. Sự không nhất quán giữa số lượng biến và mục trong danh sách gây ra lỗi

Nếu chuỗi của bạn bắt đầu bằng hoặc kết thúc bằng dấu phân cách cụ thể, bạn sẽ nhận được các phần tử chuỗi trống trong danh sách

Bạn có thể sử dụng hàm

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

4 để xóa bất kỳ chuỗi trống nào khỏi danh sách

Hàm bộ lọc lấy một hàm và một iterable làm đối số và xây dựng một iterator từ các phần tử của iterable mà hàm trả về một giá trị trung thực

Nếu bạn vượt qua

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

5 cho đối số hàm, thì tất cả các phần tử giả mạo của lần lặp sẽ bị xóa

Tất cả các giá trị không trung thực được coi là giả. Các giá trị giả trong Python là

  • hằng số được xác định là sai.
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    5 và
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    7
  • coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    8 (không) của bất kỳ loại số nào
  • trình tự và bộ sưu tập trống.
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    00 (chuỗi trống),
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    01 (bộ trống),
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    02 (danh sách trống),
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    03 (từ điển trống),
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    04 (bộ trống),
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    05 (phạm vi trống)

Lưu ý rằng hàm

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

4 trả về một đối tượng
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

07, vì vậy chúng ta phải sử dụng lớp
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

08 để chuyển đổi đối tượng
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

07 thành một danh sách

Đầu tiên, tôi sẽ giới thiệu cho bạn cú pháp của phương thức

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9. Sau đó, bạn sẽ thấy cách sử dụng phương thức
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 có và không có đối số, sử dụng các ví dụ mã trong quá trình thực hiện

Đây là những gì chúng tôi sẽ đề cập

  1. Cú pháp phương thức
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    9
    1. Phương thức
      coding_journey = "I am learning to code for free with freeCodecamp!"
      
      # split string into a list and save result into a new variable
      coding_journey_split = coding_journey.split()
      
      print(coding_journey)
      print(coding_journey_split)
      
      # check the data type of coding_journey_split by using the type() function
      print(type(coding_journey_split))
      
      # output
      # I am learning to code for free with freeCodecamp!
      # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
      # 
      
      
      9 hoạt động như thế nào mà không có bất kỳ đối số nào?
    2. Phương thức
      coding_journey = "I am learning to code for free with freeCodecamp!"
      
      # split string into a list and save result into a new variable
      coding_journey_split = coding_journey.split()
      
      print(coding_journey)
      print(coding_journey_split)
      
      # check the data type of coding_journey_split by using the type() function
      print(type(coding_journey_split))
      
      # output
      # I am learning to code for free with freeCodecamp!
      # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
      # 
      
      
      9 hoạt động như thế nào với đối số
      coding_journey = "I am learning to code for free with freeCodecamp!"
      
      # split string into a list and save result into a new variable
      coding_journey_split = coding_journey.split()
      
      print(coding_journey)
      print(coding_journey_split)
      
      # check the data type of coding_journey_split by using the type() function
      print(type(coding_journey_split))
      
      # output
      # I am learning to code for free with freeCodecamp!
      # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
      # 
      
      
      2?
    3. Phương thức
      coding_journey = "I am learning to code for free with freeCodecamp!"
      
      # split string into a list and save result into a new variable
      coding_journey_split = coding_journey.split()
      
      print(coding_journey)
      print(coding_journey_split)
      
      # check the data type of coding_journey_split by using the type() function
      print(type(coding_journey_split))
      
      # output
      # I am learning to code for free with freeCodecamp!
      # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
      # 
      
      
      9 hoạt động như thế nào với đối số
      coding_journey = "I am learning to code for free with freeCodecamp!"
      
      # split string into a list and save result into a new variable
      coding_journey_split = coding_journey.split()
      
      print(coding_journey)
      print(coding_journey_split)
      
      # check the data type of coding_journey_split by using the type() function
      print(type(coding_journey_split))
      
      # output
      # I am learning to code for free with freeCodecamp!
      # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
      # 
      
      
      4?

Phương thức coding_journey = "I am learning to code for free with freeCodecamp!" # split string into a list and save result into a new variable coding_journey_split = coding_journey.split() print(coding_journey) print(coding_journey_split) # check the data type of coding_journey_split by using the type() function print(type(coding_journey_split)) # output # I am learning to code for free with freeCodecamp! # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!'] # 9 trong Python là gì?

Bạn sử dụng phương pháp

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 để tách một chuỗi thành một danh sách

Cú pháp chung của phương thức

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 giống như sau

string.split(separator, maxsplit)

Hãy phá vỡ nó

  • coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    9 là chuỗi bạn muốn tách. Đây là chuỗi mà bạn gọi phương thức
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    9
  • Phương thức
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    9 chấp nhận hai đối số
  • Đối số tùy chọn đầu tiên là
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    2, chỉ định loại dấu tách nào sẽ được sử dụng để tách chuỗi. Nếu đối số này không được cung cấp, thì giá trị mặc định là bất kỳ khoảng trắng nào, nghĩa là chuỗi sẽ tách ra bất cứ khi nào
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    9 gặp bất kỳ khoảng trắng nào
  • Đối số tùy chọn thứ hai là
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    4, chỉ định số lượng phân tách tối đa mà phương thức
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    9 sẽ thực hiện. Nếu đối số này không được cung cấp, thì giá trị mặc định là
    coding_journey = "I love   coding"
    
    coding_journey_split = coding_journey.split()
    
    print(coding_journey_split)
    
    # output
    # ['I', 'love', 'coding']
    
    6, nghĩa là không có giới hạn về số lần phân tách và
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    9 sẽ phân tách chuỗi trên tất cả các lần xuất hiện mà nó gặp phải
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    2

Phương thức

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 trả về một danh sách chuỗi con mới và chuỗi ban đầu không bị sửa đổi theo bất kỳ cách nào

Phương pháp coding_journey = "I am learning to code for free with freeCodecamp!" # split string into a list and save result into a new variable coding_journey_split = coding_journey.split() print(coding_journey) print(coding_journey_split) # check the data type of coding_journey_split by using the type() function print(type(coding_journey_split)) # output # I am learning to code for free with freeCodecamp! # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!'] # 9 hoạt động như thế nào mà không có bất kỳ đối số nào?

Đây là cách bạn chia một chuỗi thành một danh sách bằng cách sử dụng phương thức

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 mà không có bất kỳ đối số nào

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

Đầu ra cho thấy rằng mỗi từ tạo nên chuỗi hiện là một mục danh sách và chuỗi gốc được giữ nguyên

Khi bạn không chuyển một trong hai đối số mà phương thức

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 chấp nhận, thì theo mặc định, nó sẽ tách chuỗi mỗi khi gặp khoảng trắng cho đến khi chuỗi kết thúc

Điều gì xảy ra khi bạn không chuyển bất kỳ đối số nào cho phương thức

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 và nó gặp các khoảng trắng liên tiếp thay vì chỉ một?

coding_journey = "I love   coding"

coding_journey_split = coding_journey.split()

print(coding_journey_split)

# output
# ['I', 'love', 'coding']

Trong ví dụ trên, tôi đã thêm các khoảng trắng liên tiếp giữa từ

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

04 và từ
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

05. Trong trường hợp này, phương thức
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 xử lý bất kỳ khoảng trắng liên tiếp nào như thể chúng là một khoảng trắng duy nhất

Phương pháp coding_journey = "I am learning to code for free with freeCodecamp!" # split string into a list and save result into a new variable coding_journey_split = coding_journey.split() print(coding_journey) print(coding_journey_split) # check the data type of coding_journey_split by using the type() function print(type(coding_journey_split)) # output # I am learning to code for free with freeCodecamp! # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!'] # 9 hoạt động như thế nào với đối số coding_journey = "I am learning to code for free with freeCodecamp!" # split string into a list and save result into a new variable coding_journey_split = coding_journey.split() print(coding_journey) print(coding_journey_split) # check the data type of coding_journey_split by using the type() function print(type(coding_journey_split)) # output # I am learning to code for free with freeCodecamp! # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!'] # 2?

Như bạn đã thấy trước đó, khi không có đối số

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2, giá trị mặc định cho nó là khoảng trắng. Như đã nói, bạn có thể đặt một
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2 khác

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2 sẽ ngắt và chia chuỗi bất cứ khi nào gặp ký tự bạn chỉ định và sẽ trả về danh sách các chuỗi con

Ví dụ: bạn có thể làm cho chuỗi tách ra bất cứ khi nào phương thức

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 gặp dấu chấm,
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

63

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

0

Trong ví dụ trên, chuỗi tách ra bất cứ khi nào

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 gặp một
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

63

Hãy nhớ rằng tôi đã không chỉ định một dấu chấm theo sau bởi một khoảng trắng. Điều đó sẽ không hoạt động vì chuỗi không chứa dấu chấm theo sau là khoảng trắng

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

6

Bây giờ, hãy xem lại ví dụ cuối cùng từ phần trước

Khi không có đối số

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2, các khoảng trắng liên tiếp được xử lý như thể chúng là các khoảng trắng đơn lẻ

Tuy nhiên, khi bạn chỉ định một khoảng trắng là

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2, thì chuỗi sẽ tách ra mỗi khi gặp một ký tự khoảng trắng

string.split(separator, maxsplit)
2

Trong ví dụ trên, mỗi lần

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 gặp một ký tự khoảng trắng, nó sẽ tách từ đó ra và thêm khoảng trống dưới dạng một mục danh sách

Phương pháp coding_journey = "I am learning to code for free with freeCodecamp!" # split string into a list and save result into a new variable coding_journey_split = coding_journey.split() print(coding_journey) print(coding_journey_split) # check the data type of coding_journey_split by using the type() function print(type(coding_journey_split)) # output # I am learning to code for free with freeCodecamp! # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!'] # 9 hoạt động như thế nào với đối số coding_journey = "I am learning to code for free with freeCodecamp!" # split string into a list and save result into a new variable coding_journey_split = coding_journey.split() print(coding_journey) print(coding_journey_split) # check the data type of coding_journey_split by using the type() function print(type(coding_journey_split)) # output # I am learning to code for free with freeCodecamp! # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!'] # 4?

Khi không có đối số

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

4, sẽ không có giới hạn cụ thể về thời điểm ngừng phân tách

Trong ví dụ đầu tiên của phần trước,

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9 tách chuỗi mỗi lần nó gặp
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2 cho đến khi chạm đến cuối chuỗi

Tuy nhiên, bạn có thể chỉ định thời điểm bạn muốn quá trình phân tách kết thúc

Ví dụ: bạn có thể chỉ định rằng phần tách kết thúc sau khi nó gặp một dấu chấm

string.split(separator, maxsplit)
6

Trong ví dụ trên, tôi đặt

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

4 thành
string.split(separator, maxsplit)
25 và một danh sách được tạo với hai mục danh sách

Tôi đã chỉ định rằng danh sách sẽ tách ra khi gặp một dấu chấm. Khi nó gặp một dấu chấm, hoạt động sẽ kết thúc và phần còn lại của chuỗi sẽ là một mục danh sách của chính nó

Phần kết luận

Và bạn có nó rồi đấy. Bây giờ bạn đã biết cách tách một chuỗi trong Python bằng phương thức

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

9

Tôi hy vọng bạn thấy hướng dẫn này hữu ích

Để tìm hiểu thêm về ngôn ngữ lập trình Python, hãy xem chứng chỉ Python của freeCodeCamp

Bạn sẽ bắt đầu từ những điều cơ bản và học theo cách tương tác và thân thiện với người mới bắt đầu. Cuối cùng, bạn cũng sẽ xây dựng năm dự án để đưa vào thực tế và giúp củng cố những gì bạn đã học

Cảm ơn bạn đã đọc và chúc bạn mã hóa vui vẻ

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO


Làm thế nào để bạn chia một chuỗi thành hai trong python?
Dionysia Lemonaki

Học một cái gì đó mới mỗi ngày và viết về nó


Nếu bài viết này hữu ích, hãy tweet nó

Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu