0% found this document useful (0 votes)
5 views2 pages

2025 Python Short String Methods-720p-En

The document discusses string methods in Python, focusing on cleaning up show titles from the '90s by addressing common formatting issues such as leading/trailing spaces and capitalization. It demonstrates the use of methods like capitalize, title, strip, and join to format strings effectively. The final output shows how to create a cleaned list of show titles, properly formatted for display.

Uploaded by

angelyou641
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

2025 Python Short String Methods-720p-En

The document discusses string methods in Python, focusing on cleaning up show titles from the '90s by addressing common formatting issues such as leading/trailing spaces and capitalization. It demonstrates the use of methods like capitalize, title, strip, and join to format strings effectively. The final output shows how to create a cleaned list of show titles, properly formatted for display.

Uploaded by

angelyou641
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

[AUDIO LOGO]

Well, hello, one and all.


And welcome to our short on string methods.
I have here a list of strings representing titles of shows
for children in the '90s or so.
So you might notice that a few of these show titles
aren't really appropriately styled.
I have some spaces at the beginning, even at the end.
I have some mixed capitalization.
These are all common mistakes you might get when you work with data,
particularly user entered data.
So let's see how we could clean up a little bit these titles of shows.
I have down below a main function that iterates
over every show in this list of shows, currently
just printing it out to my terminal.
So I'll go ahead and run Python of shows.py.
And I'll see all of these show names down below in my terminal.
But my goal is to clean these up a little bit.
I want to make sure they have the appropriate capitalization, that we're
removing extra spaces.
So this is a great job for string methods.
Again, methods here refer to functions that
belong to some kind of object, in this case, in particular a string.
So if I have a string, I can use a variety of methods to clean it up
or these functions that belong to strings.
Now, what I want to show you is one called capitalize.
And I can take each string here referred to as show
and capitalize it when I print it out.
I could do show.capitalize using this method by using dot notation here,
taking my string--
the name of it, at least--
dot, and then the method name, capitalize.
Here, if I run Python of shows.py, let's see what we get.

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.

You might also like