Changes

Jump to: navigation, search

OPS435 Python Lab 4

1,466 bytes removed, 23:14, 11 February 2018
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))
</source>Strings can also use special syntax for string '''repetition''' by <u>multiplying</u> the string by a number. This will repeat that string that many times. Repetition with '''*''' is useful whenever a string needs to be repeated more than once<br><br>:#Issue the following:<source>print(course_name + '-' + course_code)print(course_name + '-'*5 + course_code)print(course_name + '-'*25 + course_code)print('abc'*2)print(course_code*5)</source>When using the '''print()''' function, you can display '''special characters'''. One such special character is the is the newline character (denoted by the symbol: '''\n'''). This allows you to separate content between new lines or empty lines.<br><br>:#To demonstrate, issue the following:<sourcelang="python">
print('Line 1\nLine 2\nLine 3\n')
</source>
:#By using both string repetition and a newline character, multiple lines can be created at once. Issue the following:<source>print('Line 1' + '\n'*4 + 'Line 5\nLine 6')</source>:#Strings have many built-in functions that we can use to manipulate text. Let[https://docs.python.org/3/library/stdtypes.html#string-methods Here's take a look at the strings name space and the available functions:<source>dir(course_name)help(course_name)</source>list].:#Lets try out several different functions. Refer back to the '''help()''' function for more information, these are quick ways to view strings in different ways. Issue the following:<sourcelang="python">
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]
first_word
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

Navigation menu