Page 1 of 1

Python: Splitting a String

Posted: Tue Apr 07, 2026 5:49 pm
by Jason

Code: Select all

# Splitting a string into a list

string_to_be_split = str("Bob Berry Chuck, Andrew Amber Mary")

name_list_from_split_string = string_to_be_split.split()

print(name_list_from_split_string)

# Note that be default it's split on whitespace characters with no limit

Code: Select all

import re

# I find this advanced way more useful.

string_to_be_split = str("Amber Misty Larry Joey")

name_list_from_split_string = re.split(r '[\s+]', string_to_be_split, maxsplit = 0, flags = re.IGNORECASE)

# String is split with unlimited white spaces with case sensitivity ignored.