"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Split a String by Whitespace in Python?

How to Split a String by Whitespace in Python?

Published on 2024-11-16
Browse:566

How to Split a String by Whitespace in Python?

Whitespace Delimited Strings in Python

String handling is a common task in programming and in Python, you can easily separate whitespace Split the string at.

Using Python's str.split() method, you can split a string into a list by spaces (whitespace characters):

>>> "many   fancy word \nhello    \thi".split()
['many', 'fancy', 'word', 'hello', 'hi']

The str.split() method defaults to whitespace characters (such as spaces, tabs, and newlines) as delimiters symbol. So the above code separates the input string into a list of words separated by whitespace characters.

It is worth noting that the str.split() method can also use a regular expression as a parameter to perform more precise segmentation. However, for simple whitespace separation, the default behavior is sufficient.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3