Có cách nào để chuyển các từ trong string thành array trong python không?

Có cách nào để chuyển các từ trong string thành các mục trong array trong python không?

Ví dụ em có một câu như sau:

indent preformatted text by 4 spaces

làm thế nào để tự động chuyển thành array ntn:

["indent", "preformatted", "text", "by", "4", "spaces"]

Em xin cảm ơn

Có.

s = "indent preformatted text by 4 spaces"
print(s.split(" "))  # hay s.split()
3 Likes

Bạn sử dụng hàm split() nhé:

a="indent preformatted text by 4 spaces"
b=a.split(" ")
print("list b=",b)

kq sẽ trả về:

list b=["indent", "preformatted", "text", "by", "4", "spaces"]
1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?