Python split string by space | Example code

The split() method to splits a string by space in Python. You can use any separator like a comma, a hyphen, etc, the default separator is any space.

string.split(separator, maxsplit) 

Note: Separator and max split are optional parameters.

Example Split string on whitespace in Python

Simple example code split a string into a list.

txt = "Welcome to the Eyehunts"

x = txt.split()

print(x)

Output:

Python split string by space

Another example

split string by single space

str = '63 41 92 81 69 70'

# split string by single space
chunks = str.split(' ')

print(chunks)

Output: [’63’, ’41’, ’92’, ’81’, ’69’, ’70’]

Do comment if you have any doubts and suggestions on this Python string topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.