How do I split a string into words using Python?

DDD
Release: 2024-11-09 20:48:02
Original
887 people have browsed it

How do I split a string into words using Python?

String segmentation in Python

In Python, you can use the str.split() method to split the string according to the delimiter Or regular expression for word segmentation. By default, str.split() will split the string according to whitespace characters (including spaces, tabs, and newlines).

Use default delimiter

The following code demonstrates how to split a string into a list of words using default delimiter:

text = "many   fancy word \nhello    \thi"
words = text.split()

print(words)
# 输出:['many', 'fancy', 'word', 'hello', 'hi']
Copy after login

In this example, the string text is split into the following word list: ['many', 'fancy', 'word', 'hello', 'hi'].

Use regular expression delimiter

You can also specify a regular expression as the delimiter. This allows you to tokenize strings based on more complex patterns.

The following code demonstrates how to use regular expressions to split a string into a list of words, where whitespace characters or multiple consecutive spaces are considered delimiters:

import re

text = "many   fancy word \nhello    \thi"
white_space_regex = r"\s+"
words = re.split(white_space_regex, text)

print(words)
# 输出:['many', 'fancy', 'word', 'hello', 'hi']
Copy after login

Here In this case, the regular expression r"s" matches one or more whitespace characters, so it splits the string into a list of words, each of which has at least one whitespace character between them.

Notes

  • str.split() returns a list of strings, not a tuple.
  • If the delimiter does not exist in the string, str.split() will return a one-element list containing the original string.
  • You can specify the maximum number of splits as the second parameter of the str.split() method.

The above is the detailed content of How do I split a string into words using Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template