Changes

Jump to: navigation, search

OPS435 Python Lab 4

11 bytes removed, 23:40, 19 June 2017
PART 1 - Strings and Substrings
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. Strings can also be concatenated(combined together) by using the '''+''' sign, just make sure string are only concatenating other strings(no lists, no numbers, no dictionaries, etc)<source>
course_name
course_code
print('Line 1' + '\n'*4 + 'Line 5\nLine 6')
</source>
:#Strings have tons of functions built into them and many more we can use on them, take a look at the strings name space and the available functions<source>
dir(course_name)
help(course_name)
</source>
:#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<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 in string lowere on rest
</source>
:#These values can be saved inside new strings and reused for any new tasks<source>
lower_name = course_name.lower() # Save returned string lower-case string inside new string variable
print(lower_name)
</source>
:#If a string contains many values separated by a single character, such as a space, the string can be split on those values and create a list of values<source>
lower_name.split(' ') # Provide the split() function with a character to split on
</source>
:#The above will return a list of strings, which we can access just like all of lists<source>
list_of_strings = lower_name.split(' ') # Split string on spaces and store the list in a variable
list_of_strings # Display list
list_of_strings[0] # Display first item in list
</source>
:#Since this list is actually a list of '''strings''', any function that works on strings will work on items in the list<source>
list_of_strings[0].upper() # Use the functmon after the index to interact with just a single string in the list
first_word = list_of_strings[0]
print(first_word)
</source>
:#The index that is used inside lists is also used to access characters in a string. Take a step back from lists for now, create a new string, and start accessing the strings index.<source>
course_name = 'Open System Automation'
course_code = 'OPS435'
course_code[0] + course_code[1] + course_code[2]
</source>
:#While a list's index is for each item in the list, a string's index is for each character in the string. In a string this is called a substring, taking out a number of characters from the string and using this substring to either create a new string or display only a small portion of it<source>
course_name[0:4] # This will return the first four characters NOT including index 4 -> indexes 0,1,2,3 -> but not index 4
first_word = course_name[0:4] # Save this substring for later use
course_name[-1] # Return the last character
</source>
:#With negative indexes the index works from the right side of the string '''-1''' being the last character, '''-2''' second last character, and so on<source>
course_name = 'Open System Automation'
course_name[-1]
course_name[-2]
</source>
:# This allows string to return the substrings like below<source>
course_name = 'Open System Automation'
course_name[-10:] # Return the last ten characters
198
edits

Navigation menu