.upper(), .lower(0, and .title() all are performed on an existing string unlike.split()
Steps on a string:
.split() is performed on a string
takes on argument
returns a list of substrings found between the given argument (which in the case of .split() is known as the delimiter))
The following syntax for .split():
string_name.split(delimeter)
If you do not provide an argument for .split() it will default to splitting at spaces.
man_its_a_hot_one = "Like seven inches from the midday sun"
print(ma_its_a_hot_one.split())
# --> ['Like', 'seven', 'inches', 'from', 'the', 'midday', 'sun']
.split() returned a list with each word in the string. If we run a string with no spaces, we will get the same string in return.
If we provide an argument for .split() we can dictate the character we want our string to be split on. The argument should be provided as a string itself.
For example:
greatest_guitarist = "santana"
print(greatest_guitarist.split('n')
# --> ['sa', 'ta', 'a']
When we split a string on a character that it also ends with, you’ll end up with an empty string at the end of the list.
You can use any string as the argument for .split(), making it a versatile and powerful tool.
We can also split strings using escape sequences.
they help us indicate that we want to split by something in a string that is not necessarily a chrachter.
The two escape sequences we will cover are:
\n Newline
\t Horizontal Tab
Newline allows us to split a multi-line string by line breaks and \t will allow us to split a string by tabs. \t is useful when dealing with certain datasets.
The code is splitting the multi-line string at the newlines \n which exist at the end of each line and saving it to a new list called chrous_lines.
The new list contains each line of the original string as its own smaller string.
.join() is essentially the opposite of .split().
it joins a list of strings together with a given delimiter.
The syntax of .join() is:
'delimiter'.join(list_you_want_to_join)
For example:
my_munequita = ['My', 'Spanish', 'Harlem', 'Mona', 'Lisa']
print(' '.join(my_munequita))
# => 'My Spanish Harlem Mona Lisa'
In this example, we take the list of strings, and we joined it together with our delimiter which is a space. The space is very important if you are trying to build a sentence from words, otherwise you would have ended up with a string without spaces.
When joining strings, we can also use any string as a delimiter to join together a list of strings. For example, if we have the list:
santana_songs = ['string1', 'string2', 'string3']
We could join this list together with ANY string. One often used string is a comma , because then we can create a string of comma seperated variables, or CSV.
CSV’s are:
an efficent, simple file type used by popular programs like Excel or Google Spreadsheets
You can also join using escape sequences as the delimtier.
The code is taking the list of strings and joining them using a newline \n as the delimiter. Then it prints the result and produces the output.
When working with strings that come from real data, you will often find the strings aren’t super clean. In some cases, you’ll find lots of extra whitespace, unncessary linebreaks, and rogue tabs.
Pythoon provides a great method for cleaning strings: .strip().
Stripping a string removes all whitespace characters from the beginning and end.
Once .strip() is used, all the whitespace on either side of the string has been stripped, but the whitespace in the middle has been preserved.
You can also use .strip() with a character argument, which will string that character from either end of the string.
featuring = "!!!rob thomas. !!!"
print(featuring.strip('!'))
# --> 'rob thomas. '
NOTICE: We are no longer stripping whitespace, we are ONLY stripping the argument.
.replace() takes two arguments and replaces all instances of the first argument in a string with the second argument.
The syntax for .replace() is:
string_name.replace(substring_being_replaced, new_substring)
NOTICE: These substrings can be multiple characters long!
Another interesting string method is .find()
.find() takes a string as an argument and searching the string it was run on for that string.
It then returns the first index value where that string is located.
Here’s an example:
print('smooth'.find('t'))
# --> '4'
We searched the string ‘smooth’ for the string ‘t’ and found that it was at the fourth index spot, so /find() returned 4.
You can also search for larger strings, and .find() will return the index value of the first character of that string.
print('smooth'.find('oo'))
# --> '2'
Notice here that 2 is the index of the first o.
Python also provides a handy string method for including variables in strings known as .format().
.format() takes variables as an argument and includes them in the string that is run on. You include {} marks as placeholders for where those variables will be imported.
Consider the following functions:
def favorite_song_statement(song, artist):
return "my favorite song is {} by {}.".format(song, artist)
The function takes two arguments (song and artist), then returns a string that includes both of the arguments and prints a sentence.
NOTE: .format() can take as many arguments as there are in {} in the string it is run on, which in this case is two.
Some people may argue that you can also use concatention for doing the same thing, but for legibility and resuability it easier to picture the end result using .format().
.format() can be made even more legible for other people rading your code by including keywords. Previously, with .format(), you had to make sure that your variables appeared as arguments in the same order that you wanted them to appear in the string, which added uncecessary complications when writing code.
By including keywords in the string, and in the arguments, you can femove that ambiguity. Here’s an example:
def favorite_song_statement(song, artist):
return "My favorite song is {song} by {artist}.".format(song=song, artist=artist)
You can also reverse the order of song=song or artist=artist because you are already defining the keywords.
.upper(), .title(), and .lower() adjust the casing of your string.
.split() takes a string and creates a list of substrings.
.join() takes a list of strings and creates a a string.
.strip() cleans off whitespace, or other noise from the beginning and end of a string (not the middle)
.replace replaces all instances of a charachter or string with another charachter or string.
.find() searches a string for a chrachter/string and returns the index value that character/string is found at
.format() allows you to interpolate a string with variables.