1,234
edits
Changes
→INVESTIGATION 2: STRINGS
= INVESTIGATION 2: STRINGS =
:Strings are basically a list of characters (bits of text). Strings store text so that they can be later for manipulation (by a wide range of functions). This section will investigate strings in more detail such as '''cutting strings into sub-strings''', '''joining strings''', '''formatting strings''', '''searching through strings''', and '''matching strings against patterns'''.<br><br>Strings are '''immutable''' data objects - this means that once a string is created, it <u>cannot</u> be modified. In order to make a change inside a string, you would first make a copy of the part of the string (i.e. sub-string) for manipulation.
== PART 1 - Strings and Substrings ==
:'''Perform the Following Steps:'''
:#Launch the ipython3 shell<source>ipython3</source>:#Issue the following to create Create some stringsin a temporary Python file:<sourcelang="python">
course_name = 'Open System Automation'
course_code = 'OPS435'
course_number = '435'
</source>Strings can contain any '''characters''' inside them, whether they are '''letters''', '''numbers''', or '''symbols'''. In our ipython3 shell the values inside each string variable can be seen just by typing the string variable name. However, when writing python scripts, these string variables should be placed inside '''print()''' functions in order to display on the screen.<br><br> :#Strings can also be '''concatenated''' (i.e. "combined together") by using the '''+''' sign, just make sure string are only concatenating other strings with strings(no lists, no numbers, no dictionaries, etc.).<br><br>:#To demonstrate what was previously mentioned, issue the following:<sourcelang="python">course_namecourse_codecourse_number
print(course_name)
print(course_code)
print(str(course_number))
print(course_name + ' ' + course_code + ' ' + str(course_number))
print('Line 1\nLine 2\nLine 3\n')
</source>
course_name.lower() # Returns a string in lower-case letters
course_name.upper() # Returns a string in upper-case letters
course_name.capitalize() # Returns a string with upper-case first letter only, lowercase for remaining text
</source>
:#These values can be saved inside new strings and then reused:<sourcelang="python">
lower_name = course_name.lower() # Save returned string lower-case string inside new string variable
print(lower_name)
lower_name.split(' ') # Provide the split() function with a character to split on
</source>The above example will return a list of strings, which we can access just like all of lists. <br><br>
:#Let's practice more string manipulation by issuing the following:<sourcelang="python">
list_of_strings = lower_name.split(' ') # Split string on spaces and store the list in a variable
print(list_of_strings ) # Display listprint(list_of_strings[0] ) # Display first item in list</source>Since lists are actually a list of '''strings''', you should be able to use any function that works with a string on a list.<br><br>:#To demonstrate, issue the following:<sourcelang="python">
list_of_strings[0].upper() # Use the function after the index to affect a single string within a list
first_word = list_of_strings[0]
print(first_word)
</source>The '''index''' that is used to access <u>items</u> within a list, can also be used to access <u>characters</u> within a string. For practice, let's create a new string, and start accessing the strings index.<br><br>:#Issue the following:<source>
course_name = 'Open System Automation'
course_code = 'OPS435'
str(course_number)[0] # Turn the integer into a string, return first character in that string
course_code[0] + course_code[1] + course_code[2]
</source>:#You can use a technique that uses index numbers of a string to '''cut-out''' or '''"parse"''' smaller portions of text within a string. This term is referred to as a '''substring'''. We can use this to create a new string or display only a small portion of it<br><br>:#To demonstrate, issue the following:<sourcelang="python">
course_name[0:4] # Return the first four characters (values of index numbers 0,1,2, and 3)
first_word = course_name[0:4] # Save this substring for later use
course_code[0:3] # Return the first three characters (values of index numbers 0,1,and 2)
</source>
:# The index allows a few '''extra functions''' using the same parsing technique:<sourcelang="python">
course_name = 'Open System Automation'
course_name[12:] # Return the substring '12' index until end of string
course_name[5:] # Return the substring '5' index until end of string
course_name[-1] # Return the last character
</source>With '''negative indexesindices''', '''-1''' index would represent the '''last''' character, '''-2''' index would represent the '''second last''' character, etc.<br><br>:#To demonstrate, issue the following:<sourcelang="python">
course_name = 'Open System Automation'
course_name[-1]
course_name[-2]
</source>
:# Issue the following to practice Practice some of the skills that you have learned in this section:<source>
course_name = 'Open System Automation'
course_name[-10:] # Return the last ten characters