Làm thế nào để bạn tách một chuỗi vào lần xuất hiện cuối cùng của một ký tự trong python?

Đầu tiên, tôi sẽ giới thiệu cho bạn cú pháp của phương thức .split(). Sau đó, bạn sẽ thấy cách sử dụng phương thức .split() 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

Phương thức .split() trong Python là gì?

Bạn sử dụng phương pháp .split() để tách một chuỗi thành một danh sách

Cú pháp chung của phương thức .split() 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 .split()
  • Phương thức .split() 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 sẽ 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 .split() 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 .split() 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à .split() 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 .split() 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 .split() hoạt động như thế nào mà không có bất kỳ đối số nào?

Đây là cách bạn sẽ tách một chuỗi thành một danh sách bằng cách sử dụng phương thức .split() 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 .split() 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 .split() 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ừ

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(".")

print(fave_website_split)

# output
# ['www', 'freecodecamp', 'org']
4 và từ
fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(".")

print(fave_website_split)

# output
# ['www', 'freecodecamp', 'org']
5. Trong trường hợp này, phương thức .split() 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 .split() 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 .split() gặp dấu chấm,

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(". ")

print(fave_website_split)

# output
# ['www.freecodecamp.org']
3

________số 8_______

Trong ví dụ trên, chuỗi tách ra bất cứ khi nào .split() gặp một ____14_______3

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

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(". ")

print(fave_website_split)

# output
# ['www.freecodecamp.org']

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à khoảng trắng đơn

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

coding_journey = "I love   coding"

coding_journey_split = coding_journey.split(" ")

print(coding_journey_split)

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

Trong ví dụ trên, mỗi lần .split() 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 .split() 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, .split() 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

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(".", 1)

print(fave_website_split)

# output
# ['www', 'freecodecamp.org']

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
coding_journey = "I love   coding"

coding_journey_split = coding_journey.split(" ")

print(coding_journey_split)

# output
# ['I', 'love', '', '', 'coding']
5 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 .split()

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 tách một chuỗi vào lần xuất hiện cuối cùng của một ký tự 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