Page 1 of 1

Python: Splitting a String

Posted: Mon Sep 15, 2025 6:22 pm
by The Jason

Code: Select all

# Splitting a string into a list

string_to_be_split = "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 = "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.