Useful methods ISlide solved ✓
Useful string methods
This lesson covers a variety of useful methods on Python strings.
It is meant as a way to practice method calls. You don't need to memorize every method.
Simply having heard of a method and being able to look it up later could save you time in the future.
Lower- and uppercase strings
As you saw in the previous lesson, strings have
that return upper- and lower-case versions of the string.
The code below is an example. Run it to continue.
Try using upper and lower
Write a function named
change_case that takes two parameters:
string2. Your function should convert
string1 to uppercase,
lowercase. Concatenate the two strings with a space in between and return this
change_case("hello", "world") == "HELLO world" change_case("oh", "okay") == "OH okay"
Strings have a method named
split that splits the string into words. If the
method is called with no arguments, any whitespace (such as spaces
or tabs) are used as separator. For example:
"hello spaces".split() == ['hello', 'spaces']
If you pass a string to
split, the letters in that string will be used as
separator. For example:
"Alice, 30, green".split(", ") == ['Alice', '30', 'green']
Try splitting a string
Try to use the
split method. Write a function named
takes a string as parameter and splits it by the
| character, then returns
split_by_bar("x|y|z") == ["x", "y", "z"]
Strings have a
strip method that removes all whitespace (such as spaces or
tabs) from the beginning and end of a string. This can be very handy if you
are reading input from the user and you want to clean it up.
" spaces around this ".strip() == "spaces around this"
Use string strip
Write a function named
needs_cleanup that takes a single string as a
parameter. The function should call the
strip method on the string and
compare the result to the original. Return
True if the two are different and
the string therefore needs a cleanup. Return
needs_cleanup("hello") == False needs_cleanup(" hello") == True
Quite often, you'll need to combine many strings, putting a space in between each. You could do this with a loop, but the code would get messy and hard to understand.
As an alternative, strings have a
join method. You call this on the string
that should go in between other strings. The argument to the method call is
the list of strings that should be concatenated.
If that sounds complicated, some examples might clarify:
" ".join(["hello", "world"]) == "hello world" " ".join(["a", "b", "c"]) == "a b c"
Try joining strings
Try using the
join method below. Your code should define a function named
join_by_space that takes a list of strings as its parameter. It should join
these strings with spaces in between and return the result.
join_by_space(["hi", "there"]) == "hi there"
Starts with and ends with
Strings have the methods
endswith. For example:
"hello".startswith("he") == True "hello".endswith("o") == True
Try it below. Write a method named
test_b that returns True if its string
parameter starts with or ends with
"b" and False otherwise.