Python: Splitting a String

Post Reply
User avatar
Jason
Site Admin
Posts: 767
Joined: Sun Dec 21, 2025 8:56 pm

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.
 

POSTREACT(ions) SUMMARY

Post Reply