Locked lesson.
About this lesson
How to manipulate strings.
Exercise files
Download this lesson’s related exercise files.
String Manipulation.docx58.8 KB String Manipulation - Solution.docx
58.9 KB
Quick reference
String Manipulation
In this video we'll learn how to do some string manipulation.
When to use
There are always times when you'll want to manipulate strings. Here's how to do it.
Instructions
There are many ways to manipulate strings.
- .upper() = changes everything to uppercase
- .lower() = changes everything to lowercase
- .title() = changes the first letter of each word to uppercase
- .capitalize() = capitalizes just the first letter of the first word
- .swapcase() = reverses the case of every letter
To find the length of a string, wrap it in the len function:
print(len(your_variable))
Print a range of characters from a string:
print(your_variable[0:5])
Split apart your string by its spaces:
print(your_variable.split(' '))
Hints & tips
- There are many ways to manipulate strings
- 00:04 In this video, I want to talk about string manipulation.
- 00:07 How to do things with strings.
- 00:09 So let's create a string.
- 00:11 Let's just call it again my_string and
- 00:15 let's say, my name is John Elder.
- 00:18 And if we want to print this obviously, we just go my_string, save this, run it.
- 00:24 We know how to do this.
- 00:25 My name is John Elder.
- 00:26 So there are lots of different ways we can manipulate this string programmatically
- 00:31 and we can use all kinds of object oriented type things.
- 00:35 A string is an object, we could do stringy things to it and with Python,
- 00:39 in order to do stringy things, one of the ways is to stick a period and
- 00:44 then type the thing afterwards that you want to do.
- 00:46 So let's change this all from lowercase to uppercase.
- 00:51 It would just go U-P-P-E-R, upper, and then these two little brackets.
- 00:56 We save this and run it.
- 00:57 We get, MY NAME IS JOHN ELDER, all capital letters.
- 01:00 So that's kind of neat.
- 01:01 Very, very simple to do.
- 01:02 So we can do the same thing but lowercase,
- 01:05 we can lowercase all of the letters on there.
- 01:07 You can see right now it's John and Elder are capitalized.
- 01:11 But if we run this, everything is lowercase.
- 01:13 So upper lower.
- 01:15 One interesting one is title.
- 01:17 Let's see what this one does.
- 01:18 We run this, we see every letter is capitalized.
- 01:21 Now that's not to be confused with capitalization.
- 01:24 Capitalization would just capitalize this very first one and
- 01:27 leave the rest of the same but with title, sort of like a headline in a newspaper,
- 01:31 all the main words are capitalized.
- 01:34 So speaking of capitalize, let's look at that one, capitalize, save, run.
- 01:41 Like I said, just the very first letter gets capitalized,
- 01:44 the rest remain lower case.
- 01:45 And finally, an interesting one is swapcase.
- 01:49 And what this will do is reverse the case of everything and see, so like before,
- 01:54 everything was lowercase, but the J and the E, now everything is uppercase, but
- 01:58 the J and the E.
- 02:00 The cases have been swapped.
- 02:02 So those are a few very simple ways to manipulate strings like that.
- 02:06 We can also sort of dig in to a string and do interesting things.
- 02:10 Pull things out of it programmatically and that's pretty useful.
- 02:14 What we do is sort of like with a list or a tuple we put these square brackets and
- 02:20 we call out the number so m is 0, y is 1, this space is 2,
- 02:24 the n is 3, and on and on.
- 02:30 So if we wanted to pull out the y, that would be the 0,1, 1-th thing.
- 02:35 So if we save this, print it, we get this y.
- 02:38 So that's kind of neat, but it's not all that useful.
- 02:41 We need to know how many characters are in the string completely.
- 02:44 So we can call the len method to get the length.
- 02:46 So it's just l-e-n and we wrap this whole thing, like this.
- 02:52 Let's remove that to find the length, so there are 21 characters in this string.
- 02:58 So that's kind of useful.
- 02:59 Now, we can actually do a range.
- 03:01 It's kind of interesting.
- 03:03 If we go from 0 to 5, we save this and run it, it's my na.
- 03:11 So this is 0, 1, 2, 3, 4.
- 03:14 It goes up to 5 but it does not do 5.
- 03:17 Okay, we can also go len, and then name our variable again my_string so
- 03:23 now it's going to be from 0 to the end, the length.
- 03:27 Save this, it should prompt the whole thing.
- 03:29 My name is John Elder, so say you only wanted to print out name is John Elder.
- 03:34 Well, you could go 0, 1, 2, 3 name is John Elder.
- 03:39 So all kinds of ways you can play around with this kind of interesting.
- 03:43 The last thing I want to look at very quickly is,
- 03:47 we can actually split this thing apart and we can split it any way we want.
- 03:51 So we've got a bunch of spaces, let's go ahead and split this by the spaces.
- 03:55 So we can go my_string.split, and then pass in a parameter of a space.
- 04:01 If we save this and print it,
- 04:03 it looks just like the list that we looked at earlier.
- 04:07 And in fact, we can call items out of here like with the list just by slapping
- 04:11 on those brackets at the end so say, we want the 0, 1, 2,
- 04:15 3rd thing we just slap a 3 in there, save it and it prints out John.
- 04:19 So all kinds of weird neat things you can do with string manipulation,
- 04:22 these are just a few sort of basic, obvious things to do if you want to look
- 04:26 up string manipulation, you can find all kinds of other things you can do.
- 04:30 But that pretty much sums it up for this video.
- 04:32 In the next video, we're going to look at Math operators.
Lesson notes are only available for subscribers.