Python 3 Notes: Split and Join (2024)

On this page: .split(), .join(), and list().

Splitting a Sentence into Words: .split()

Below, mary is a single string. Even though it is a sentence, the words are not represented as discreet units. For that, you need a different data type: a list of strings where each string corresponds to a word. .split() is the method to use:
>>> mary = 'Mary had a little lamb'>>> mary.split() ['Mary', 'had', 'a', 'little', 'lamb'] 
.split() splits mary on whitespce, and the returned result is a list of words in mary. This list contains 5 items as the len() function demonstrates. len() on mary, by contrast, returns the number of characters in the string (including the spaces).
>>> mwords = mary.split() >>> mwords['Mary', 'had', 'a', 'little', 'lamb'] >>> len(mwords) # number of items in mwords5 >>> len(mary) # number of characters22 
Whitespace characters include space ' ', the newline character '\n', and tab '\t', among others. .split() separates on any combined sequence of those characters:
>>> chom = ' colorless green \n\tideas\n' # ' ', '\n', '\t' bunched up>>> print(chom) colorless green ideas >>> chom.split()['colorless', 'green', 'ideas'] 

Splitting on a Specific Substring

By providing an optional parameter, .split('x') can be used to split a string on a specific substring 'x'. Without 'x' specified, .split() simply splits on all whitespace, as seen above.
>>> mary = 'Mary had a little lamb'>>> mary.split('a') # splits on 'a'['M', 'ry h', 'd ', ' little l', 'mb'] >>> hi = 'Hello mother,\nHello father.'>>> print(hi)Hello mother,Hello father. >>> hi.split() # no parameter given: splits on whitespace['Hello', 'mother,', 'Hello', 'father.'] >>> hi.split('\n') # splits on '\n' only['Hello mother,', 'Hello father.'] 

String into a List of Characters: list()

But what if you want to split a string into a list of characters? In Python, characters are simply strings of length 1. The list() function turns a string into a list of individual letters:
>>> list('hello world')['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] 
More generally, list() is a built-in function that turns a Python data object into a list. When a string type is given, what's returned is a list of characters in it. When other data types are given, the specifics vary but the returned type is always a list. See this tutorial for details.

Joining a List of Strings: .join()

If you have a list of words, how do you put them back together into a single string? .join() is the method to use. Called on a "separator" string 'x', 'x'.join(y) joins every element in the list y separated by 'x'. Below, words in mwords are joined back into the sentence string with a space in between:
>>> mwords['Mary', 'had', 'a', 'little', 'lamb'] >>> ' '.join(mwords)'Mary had a little lamb' 
Joining can be done on any separator string. Below, '--' and the tab character '\t' are used.
>>> '--'.join(mwords)'Mary--had--a--little--lamb' >>> '\t'.join(mwords)'Mary\thad\ta\tlittle\tlamb' >>> print('\t'.join(mwords))Mary had a little lamb 
The method can also be called on the empty string '' as the separator. The effect is the elements in the list joined together with nothing in between. Below, a list of characters is put back together into the original string:
>>> hi = 'hello world'>>> hichars = list(hi)>>> hichars['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] >>> ''.join(hichars)'hello world' 
Python 3 Notes: Split and Join (2024)
Top Articles
How to Slash Your Bills: 4 Steps to Paying Less Each Month - Man vs Debt
Compare Current Mortgage Rates in March 2024
Evil Dead Movies In Order & Timeline
Prosper TX Visitors Guide - Dallas Fort Worth Guide
Craigslist Free Grand Rapids
What’s the Difference Between Cash Flow and Profit?
Osrs Blessed Axe
Echo & the Bunnymen - Lips Like Sugar Lyrics
Shreveport Active 911
How Much Are Tb Tests At Cvs
Xxn Abbreviation List 2023
Spider-Man: Across The Spider-Verse Showtimes Near Marcus Bay Park Cinema
MLB power rankings: Red-hot Chicago Cubs power into September, NL wild-card race
Unforeseen Drama: The Tower of Terror’s Mysterious Closure at Walt Disney World
What Channel Is Court Tv On Verizon Fios
Egizi Funeral Home Turnersville Nj
Lines Ac And Rs Can Best Be Described As
Scripchat Gratis
Mals Crazy Crab
Craiglist.nj
Best Middle Schools In Queens Ny
Craigslist Hunting Land For Lease In Ga
Things to do in Pearl City: Honolulu, HI Travel Guide by 10Best
Korg Forums :: View topic
Mawal Gameroom Download
Craigslist Sf Garage Sales
Sam's Club Near Wisconsin Dells
Street Fighter 6 Nexus
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
"Pure Onyx" by xxoom from Patreon | Kemono
Little Caesars Saul Kleinfeld
Imperialism Flocabulary Quiz Answers
Bismarck Mandan Mugshots
دانلود سریال خاندان اژدها دیجی موویز
Mixer grinder buying guide: Everything you need to know before choosing between a traditional and bullet mixer grinder
Craigs List Palm Springs
Japanese Big Natural Boobs
My Locker Ausd
Stewartville Star Obituaries
Ig Weekend Dow
Sound Of Freedom Showtimes Near Lewisburg Cinema 8
Arcane Bloodline Pathfinder
Hovia reveals top 4 feel-good wallpaper trends for 2024
Alston – Travel guide at Wikivoyage
Sallisaw Bin Store
Craigslist Binghamton Cars And Trucks By Owner
10 Types of Funeral Services, Ceremonies, and Events » US Urns Online
Kjccc Sports
Gonzalo Lira Net Worth
Marcel Boom X
53 Atms Near Me
Cognitive Function Test Potomac Falls
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6252

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.