-
PROFESSOR: Strings are an
essential data type in Python
-
that are used in nearly
every application.
-
And in this tutorial, we
learn about the most essential
-
built-in string methods.
-
So after watching
this video, you
-
will be equipped with
all the knowledge
-
you need to work with strings
easily and modify them
-
without any problems.
-
So let's get started.
-
Number 1 is slicing.
-
So with slicing, we
can use this syntax.
-
And this means we
access a substring.
-
So now, if we run this, we
still get the whole string.
-
But now we can put in an
optional start and also
-
an optional stop index.
-
So now if we run this, this
means we get only the substring
-
from position 3 to position
7, and 8 is excluded.
-
Number 2 is the strip method.
-
So by default, this will
remove all leading and trailing
-
whitespace characters.
-
So, for example, spaces,
tabs, and newline characters.
-
So if we run this,
we simply get "hello"
-
without the spaces in the
beginning and the end.
-
By default, strip removes
all leading and trailing
-
whitespace characters.
-
But we can also put
in a character here.
-
And now it removes all leading
and trailing given characters.
-
So now, if we run
it, then it removes
-
the leading and trailing hashes.
-
But be careful here
because sometimes we forget
-
that this removes only leading
and trailing found matches.
-
So if we run this, then it still
has the whitespace characters
-
in the beginning.
-
For example, this
new line and the tab.
-
And this is because the very
first leading sign is not
-
a found match for this.
-
So if we put it
like this, then we
-
have a leading whitespace,
a leading new line
-
character again.
-
So now, if we run this,
then this got removed.
-
We can also pass in multiple
characters to the strip method,
-
but now it will not interpret
this as one single string.
-
But instead, it will look for
all the occurrences of all
-
the single given characters.
-
For example, here it will remove
all the c's, m's, o's, w's,
-
and all dots.
-
So for this string,
it removes all
-
the W's and the dot
in the beginning
-
and then also this dot
and the c, o, and the m.
-
So if we run this, we only
get example as a string back.
-
Number 3 and 4 are
lstrip and rstrip.
-
So if we only want to remove
white spaces on one side,
-
then we could, for
example, use lstrip.
-
This means we remove only
the leading white space
-
so we can see we still have
the trailing white space
-
in our output.
-
Or if we use rstrip, then
we remove only the trailing
-
whitespace characters.
-
So now we see we still have
the spaces in the beginning.
-
Number 5 and 6 are remove
prefix and remove suffix.
-
So like I said, if we put in
multiple characters to a strip
-
function, then it removes all
the single given characters.
-
So in this example, it removes
also this t, this h, and this r.
-
So if we run this, then we
get only this part back.
-
And if we are really
looking to only remove
-
this particular substring
in the beginning,
-
then we can use remove prefix.
-
So if we run this, then we
only get this part back.
-
And similar, we can
use remove suffix.
-
So this will remove this part
if it finds it in the end.
-
So if we run this, then
we only get hello back.
-
Number 7 is the replace method.
-
So this will replace all the
occurrences of this given
-
string with this given string.
-
So in this case, it replaces
all the spaces with a dash.
-
So our string will
look like this.
-
And this is also sometimes
used to remove elements.
-
So if we put in an
empty string here,
-
then this will remove
all the spaces.
-
Number 8 is re.sub.
-
So this is a regular
expression method.
-
And this is useful when we
want to replace a more complex
-
pattern with a substring.
-
For example, in this string, we
want to replace all the spaces
-
with a dash.
-
But in this part, we only
want to use one dash.
-
So if we use the replace
method like before,
-
then it will replace all
the spaces with a dash.
-
So this means we get
four dashes here.
-
So in order to only
get one, we need
-
to use a regular expression.
-
And with this, we can use
re.sub and then use the pattern.
-
So this will remove multiple
whitespace characters
-
with a dash.
-
And if we assign this back
to a string and run this,
-
then our string looks like this.
-
Number 9 is the split function.
-
This will return a list of
the words in the string.
-
So in this case,
if we run this, we
-
get a list of all
the single words.
-
And by default, it uses
a space as a delimiter.
-
So for example, we can
also specify a comma here.
-
And then it would be looking
for a comma as a separator.
-
And we can also use
a max split argument.
-
So if we say max split
equals 1, then it
-
will only do one maximum split.
-
So this means we get two
strings back in our list.
-
Number 10 is our split.
-
So this works the
same as before.
-
But it starts splitting
at the right side.
-
So if we use max split
here as well and run this,
-
then it starts looking
for the first split here.
-
And then we have
this as one string
-
and this as one
string in our list.
-
So if we run this, then
this is our output.
-
Number 11 is the .join method.
-
So this is basically
for the reversed way.
-
If we first have a list of
strings and want to put this
-
back into one single string,
then we can use .join with this
-
list of strings.
-
And it uses this
string in between.
-
So if we run this,
then it puts a dash
-
in between all the
different items.
-
So here, if we use
a space, then we
-
get this as a normal sentence.
-
Then we have upper
lower and capitalize.
-
So this will put
all the characters
-
into uppercase, all the
characters into lowercase.
-
And capitalize means that
only the first character
-
is capitalized and
the rest is lowercase.
-
So if we run this, then
this is our output.
-
15 and 16 are
islower and isupper.
-
So islower returns true
if all characters are
-
lowercase characters
and false otherwise,
-
and the other way around
for the isupper method.
-
So if we run this, so this
will return true for islower.
-
And this will return
true for isupper.
-
Then we have isalpha,
isnumeric, and isalum.
-
So isalpha returns true if
all characters in the string
-
are alphabetic.
-
isnumeric returns true if
all characters in the string
-
are numeric, and
isalum returns true
-
if we have either alphabetic
or numeric characters.
-
So in this case, isalpha
and isalum returns true.
-
And if we only use numbers,
then isnumeric and isalum
-
returns true.
-
If we have both alphabetic
and numeric numbers,
-
then only isalum returns true.
-
And for example, if we
have another character
-
non-alphabetic, then none of
those functions returns true.
-
Number 20 is the count
function, so this
-
returns the number of
non-overlapping occurrences
-
of this substring
in this string.
-
So here it counts all the o's.
-
So if we run this, it returns 2.
-
If it does not find
this substring,
-
for example, this
then it returns 0.
-
Number 21 is the find function.
-
So this will return the first
lowest index in the string
-
where it finds this substring.
-
So this will return the
first index of this a.
-
So if we run this,
then the index is 1.
-
And for example, if
we use the substring,
-
then we get this string back.
-
Then, for example, if it
does not find this string,
-
then it returns minus 1.
-
So be careful here.
-
And we can also use an
optional start position.
-
So then it should start
searching at this position.
-
So in this case, it starts
looking at this position.
-
And then the next
occurrence is at index 10.
-
So now if we use this substring,
then we only get this part.
-
22 is rfind.
-
So this is the same as before.
-
But it starts searching
from the right side.
-
So in this case, it returns the
first occurrence from this side.
-
So in this case, it's also 10.
-
And then we get this substring.
-
23 and 24 are
startswith and endswith.
-
So startswith returns
true if this string starts
-
with this prefix, and
endswith returns true
-
if this string ends
with this suffix.
-
So if we run this, then both
these methods return true.
-
Number 25 is the
partition function.
-
This will split the string
at the first occurrence
-
of this given string, and
this will return a tuple
-
with three elements where the
given string is in the middle,
-
and then we have the beginning
of the string as first item
-
and the ending of the
string as last item.
-
And if it does not
find the given string,
-
then the whole string
is the first item,
-
and the other two items
are two empty strings.
-
Then we have center,
ljust, and rjust.
-
And they come in handy
when we want to format
-
our string in a certain way.
-
So center will put this string
in another string of the given
-
length, and it uses this
as the fill character.
-
So if we run this, then
we have a centered string
-
of the length 30.
-
And on both sides, we
have the fill character.
-
Then if we use ljust, then
this will be the same,
-
but our string will
be left justified.
-
So there on the
left side, and then
-
we have all the fill characters
until we reach width n.
-
And the same with rjust.
-
But here, our string
will be right justified.
-
Number 29, our f string.
-
So this is not really
a string method,
-
but this is a great new
way since Python 3.6
-
to format a string.
-
And the syntax is very easy, we
use an f and then our string.
-
And inside the string,
we can use curly braces.
-
And inside the curly braces,
we can use an expression.
-
For example, here we can
simply put in a variable.
-
Or we can also use a
mathematical expression
-
that will then be evaluated.
-
So now, if we run this,
we get this string back,
-
and yeah, I think this is a
very clear, easy, concise,
-
and more readable way
to format your strings.
-
And it's also faster than
other formatting methods.
-
Number 30 is the
swap case method.
-
So this will swap all the cases.
-
So for a uppercase, it will then
return a lowercase character
-
and then vice versa.
-
So if we run this, then
this will be our result.
-
And number 31, zfill.
-
So this will return a
string of the given length.
-
And it uses zeros to fill the
string from the left side.
-
So if we run this, then
this will be our string.
-
And also if we have a
leading plus or minus sign,
-
then this will still
be the first sign.
-
And only afterwards, it uses
zeros to fill the string.
-
And this is also very useful
if we have a number string
-
and want to format
this in a certain way.
-
All right, that's it.
-
I hope you enjoyed
this tutorial.
-
And if so, then please
hit the Like button
-
and consider subscribing
to the channel.
-
And then I hope to
see you next time.
-
Bye.