Python: Splitting a String

Post Reply
User avatar
The Jason
Site Admin
Posts: 888
Joined: Tue Jul 01, 2025 10:13 am
Contact:

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.

 

POSTREACT(ions) SUMMARY

Post Reply