Viewers: in countries Watching now:
Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. In Python 3 Essential Training, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Example projects include a normalized database interface and a complete working CRUD application. Exercise files accompany the course.
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.
Find answers to the most frequently asked questions about Python 3 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.