2025 Python Short String Methods-720p-En
2025 Python Short String Methods-720p-En
I get certainly a capital letter at the beginning, except for those with spaces,
but I don't think I've achieved my goal of making sure all of these strings
are appropriately titled.
So it turns out, capitalize-- this method actually only uppercases
the first character in your string.
In this case, I had a space.
And you can't really uppercase a space, but I don't
think this is exactly what I want.
Now, thankfully, there actually is another string method, one called title,
which will titlecase my strings for me.
So if I run Python of shows.py, let's see what happens.
Now I think I'm approaching the capitalization I wanted.
I see every word in this case capitalized in each of my strings.
In other words, these are title cased where
we assume you're capitalizing every word in the title of something.
So I'm approaching a better, more clean version of these show titles.
But I still want to get rid of these spaces on either end of our shows.
So thankfully, I can use a function.
You may have already seen one called strip.
And it is a function, but it's more specifically a method
attached to this string called show.
So I'll go ahead and do Python of shows.py.
And we'll see that now I've stripped off those pieces,
but I don't have the appropriate titling anymore.
So thankfully, I can actually chain string methods together.
If I want to not just strip my show titles of their leading and trailing
spaces, but also put them in title case, I can actually do just that.
I could do something like this-- show.strip and then title.
What this will do is take my show string, first,
strip the leading and trailing whitespace,
and then convert the result to titlecase, capitalizing every word.
I'll then run Python of shows.py.
And we'll see that I now have the best of both worlds,
thanks to this ability to chain our string methods together.
So I think these are looking pretty good.
But let's say I want to put them in a new list.
I'll do just that here.
I'll do cleaned-shows is first an empty list.
And I'll then append.
I'll append each new string to, in this case, my cleaned_shows list.
Now, if I go down below and print cleaned_shows,
I should see, fingers crossed, a new list of shows,
in this case ones that are appropriately title cased and also removed
of their leading and trailing whitespace.
But if I wanted to show the user this list of shows,
this isn't really the best format to do it in.
Here, they see the Python list syntax.
They see quotes around every string.
So there actually is a way to pretty print, if you will, this list.
I can take a string like this one, a space, for instance.
And I can use a particular string method that actually takes as input a list.
I can do .join, and I can provide as input here to join cleaned_shows.
Let's try printing this result here and see what happens.
I'll run Python of shows.py.
And now I see my shows in my list joined together, in this case by a space.
Here, I see Avatar: The Last Airbender, followed by a space, followed by Ben 10,
followed by a space, and so on throughout the rest of my list.
So join works by specifying first a string you
want to use to join your list elements.
Join then takes as an argument the list of, in this case,
shows you want to join together.
Now, let's actually change this to something like a comma.
We could then have the name of the show, comma, space, a new show, comma, space,
a new show, and so on.
I'll go down below and run Python of shows.py,
and we'll see down below a list of these shows now better formatted with commas
joining each of them together.
So this was a brief look at string methods and what you can do with them.
There are many, many more in the Python documentation.
So be sure to read about all you have at your disposal.
You've seen here, though, a few that help you clean up strings,
ones like strip, ones like title, and ones like join, too.
They can help you format your strings for printing to the user.
This then was our short on string methods.
And we'll see you next time.