Bertelsmann Tech Scholarship Challenge, Python, Udacity

Python – String Methods

There is so much that can be done with strings thanks to Methods.

Methods actually are functions that are called using dot notation. What this means is that we put them after a string. For example,

lower()

is a string method that can be used like this, on a string called “sample string”:

sample_string.lower()

A string method can only be used on a string. If we tried to use .lower() on an int or a float then we would get an error when we try to run our code.

Some methods don’t need additional methods but some do. The only way to know if they need an argument or what kind of argument is to look at the documentation.

Here are some examples or string methods.

my_string.islower()
True
my_string.count('a')
2
my_string.find('a')
3

Important Methods

format()

This method allows us to format the output from a print statement. In a way it makes it easier to concatenate a string with variables and numbers.

Without the format method if we wanted to print out a statement that was part int and part string we would have to do

print("Martha has " + str(15) + " pairs of shoes." )

That’s ok but I don’t know about you sometimes I get lazy and don’t want to type all those “” in my code. With the format method I don’t have to and it’s easier to read (in my opinion) it now looks like this

print("Martha has {} pairs of shoes".format(15))

It also works with variables instead of doing

first_name = "Claudia"
last_name = "Maciel"

print("My name is " + first_name + " " + last_name+ ".")

I can now do. The result is the same but the code is  much cleaner below.

print("My name is {} {}.".format(first_name, last_name))

split()

This is a method that gets used quite a bit. It is used to split up a string into smaller strings.

There are two arguments to this method. The first one is what we want to separate the string by. The default is whitespace.

Example maybe we have a paragraph and we wan to split it up by sentences. Our separator would be a period.

paragraph = "A measured defeated assumes the postcard. A horizontal pork fasts. The symphony troubles the much project. The goodbye installs this legitimate sniff without my bookshop."
paragraph.split('.')

our answer would be

['A measured defeated assumes the postcard', ' A horizontal pork fasts', ' The symphony troubles the much project', ' The goodbye installs this legitimate sniff without my bookshop']

The second argument that it takes is us telling us the maximum number of times to be split.

Example: Maybe we only want the first 2 words in a sentence.

sentence = "Malls are great places to shop; I can find everything I need under one roof."
sentence.split(' ', 2)

our answer would be

['Malls', 'are', 'great places to shop; I can find everything I need under one roof.']

Everything  else is left alone on the last item.

This is from Lesson 22:  Data Types and Operators Videos 19 & 20

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s