How can I split a string into a list of words in Python?

102 views

How can I split a string into a list of words in Python?

How can I split a string into a list of words in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

Use the split() method. By default, it splits on whitespace.

s = "Hello world, this is a test."
words = s.split()
print(words)  # Output: ['Hello', 'world,', 'this', 'is', 'a', 'test.']

Explanation:

  • The split() method returns a list of substrings separated by whitespace or a specified delimiter.
solveurit24@gmail.com Changed status to publish February 13, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.