Split and Join strings Python 3

show more Splitting and joining strings provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Splitting and joining strings

Splitting and joining strings is a very common operation, especially in text based environments like the World Wide Web or when you are operating on a lot of text files. So Python provides some string methods for splitting and joining strings. Let's take a look at how that works. We'll take a string. We'll put it in a variable because we are going to be using it here quite a bit. And we'll say s = 'This is a string of words.' And if we want to split that string apart we can just say s.split.

And it'll split the string apart on the white space. And in fact if there was a lot of white space in between the words, if 'tThis string was something like this is a string of words' like that .split. We see that it folds all the white space together first before splitting on the white space. So that's a very nice and handy thing to do. If we wanted to split on some other token besides the white space we can specify it in the split method but it won't do this folding thing.

That folding thing is a special thing that it does just with the white space. And it only does that if you don't specify an argument to split it all. For example if I say s.split and I want to split it on say the i characters like that, then I'll get back a list of the string with all of the i's removed and the words separated on the i characters and you see the space is still in there. So let's go ahead and split it on the white space and assign it to a variable. We'll call the variable words = s.split like that.

And now we have a list words is a list with a bunch of separate words in it. And it's utterable. I can say for w in words: print(w) and it'll print those words out separately. But I can also join them back together if I want to. I can say new string =, doing it with a colon in it, .join and so the join method operates on the token that's going to be used to join things back together not on the list of words. And so I say join and I say words like this.

And now I have a new string, which is all of those words joined back together with colons. Or if I wanted to I could say put a string with a comma and a space in it and say .join(words). And I would get back a list of words with a comma and a space. So I can really put whatever I want to there in the string that I am going to use to join the words back together. So in a nutshell that's how we split and join strings in Python. Again this is a very common operation. This is something that you'll use quite a bit if you are working with any text files.

If you are working on a World Wide Web, if you are working with mail, if you are doing any manipulation of text whatsoever. You are going to find a lot of uses for split and join.

Splitting and joining strings
Video duration: 2m 49s 6h 36m Beginner


Splitting and joining strings provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Developer Web
please wait ...