Home > Backend Development > Python Tutorial > How Can I Split a String into Words Using Multiple Delimiters in Python?

How Can I Split a String into Words Using Multiple Delimiters in Python?

DDD
Release: 2024-12-26 20:31:10
Original
697 people have browsed it

How Can I Split a String into Words Using Multiple Delimiters in Python?

Splitting Strings into Words with Multiple Word Boundary Delimiters

In Python, when splitting a string into words using str.split(), you can only specify one delimiter. This can be problematic if you want to consider punctuation as well as whitespace as word boundaries.

Solution: Using re.split()

To address this issue, consider using the re.split() function instead. re.split() allows you to specify a pattern as an argument, which can include multiple word boundary delimiters.

The pattern can be constructed using the following syntax:

\W+                  # Match any sequence of non-word characters
|                    # Or
\s+                  # Match any sequence of whitespace characters
Copy after login

To split the given example string into words, including punctuation, you can use the following code:

>>> import re
>>> re.split(r"\W+|\s+", "Hey, you - what are you doing here!?")
['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
Copy after login

The above regular expression matches any sequence of non-word characters or whitespace characters, thus effectively splitting the string into words.

This method provides a flexible and customizable way to split strings based on various delimiters, ensuring that all relevant words are captured.

The above is the detailed content of How Can I Split a String into Words Using Multiple Delimiters in 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