Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 4

67 bytes added, 23:19, 11 February 2018
PART 1 - Strings and Substrings
:#Strings have many built-in functions that we can use to manipulate text. [https://docs.python.org/3/library/stdtypes.html#string-methods Here's a list].
:#Lets try out several different functions:<source lang="python">
print(course_name.lower()) # Returns a string in lower-case lettersprint(course_name.upper()) # Returns a string in upper-case lettersprint(course_name.swapcase()) # Returns a string with upper-case and lower-case letters swappedprint(course_name.title()) # Returns a string with upper-case first letter of each word, lowercase for remaining textprint(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:<source lang="python">
course_code = 'OPS435'
course_number = 435
print(course_code[0] ) # Return a string that is Print the first character in course_codeprint(course_code[2] ) # Return a string that is Print the third character in course_codeprint(course_code[-1] ) # Return a string that is Print the last character in course_codeprint(str(course_number)[0] ) # Turn the integer into a string, return first character in that string, and print itprint(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:<source lang="python">
print(course_name[0:4] ) # Return Print 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
print(course_code[0:3] ) # Return Print 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:<source lang="python">
course_name = 'Open System Automation'
print(course_name[12:] ) # Return Print the substring '12' index until end of stringprint(course_name[5:] ) # Return Print the substring '5' index until end of stringprint(course_name[-1] ) # Return Print the last character
</source>With '''negative indices''', '''-1''' would represent the '''last''' character, '''-2''' index would represent the '''second last''' character, etc.:<source lang="python">
course_name = 'Open System Automation'
print(course_name[-1])print(course_name[-2])
</source>
:# Practice some of the skills that you have learned in this section:<source>
course_name = 'Open System Automation'
print(course_name[-10:] ) # Return the last ten charactersprint(course_name[-10:-6] ) # Try and figure out what this is returning print(course_name[0:4] + course_name[-10:-6] ) # Combine substrings together
substring = course_name[0:4] + course_name[-10:-6] # Save the combined substring as a new string for later
print(substring)
</source>
:# The real power found in substrings goes beyond just manually writing index values and getting back words. The next part of this investigation will cover how to search through a string for a specific word, letter, number, and return the index to that search result.