Changes

Jump to: navigation, search

OPS435 Python Lab 4

199 bytes added, 19:10, 21 June 2017
PART 2 - String Formatting
:#To demonstrate, issue the following:<source>
print('{} {} {}'.format('a', 'b', 'c'))
</source>However, using this method while quick, easy, and clean has a issue. If more curly braces '''{}''' are in the string than in the format() arguments, it will create a an '''error'''.<br><br>
:#To demonstrate by creating too many braces, issue the following:<source>
print('{} {} {} {}'.format('a', 'b', 'c'))
print('{0} {1} {1} {2}'.format('a', 'b', 'c'))
print('{2} {2} {1} {0}'.format('a', 'b', 'c'))
</source>:#These positions make formating each string much less prone to errors while also making the string being formatted much more clear. <br><br>:#To improve on formatting further, issue the following to provide the format() function with string variables:<source>
course_name = 'Open System Automation'
course_code = 'OPS435'
print('{0} {1} {0}'.format(course_code, course_name))
</source>:#The format() function by default tries to use values as strings, all values is '''{}''' are displayed as strings unless extra options are given.<br><br>:#Issue the following:<source>
course_number = 435 # This is an integer
print('This is displaying a string by default: {0}'.format(course_number))
</source>:#Next , let's place a list inside the format() function and access the values, there are two ways to use the list here.<br><br>:#Let's demonstrate by issuing the following:<source>
list1 = [1,2,3,4,5] # While this is a list of numbers, format() by default tried to print everything as a str()
print('{0[0]} {0[1]} {0[2]} {0[3]} {0[4]}'.format(list1)) # Access the values of the list using indexes {position[index]}
print('{} {} {} {} {}'.format(*list1)) # Use *list1 to expand the list into multiple positional arguments for format()
print('{0} {1} {2} {3} {4}'.format(1,2,3,4,5)) # Use *list1 in this situation is the same as this
</source>:#Next Let's now place a dictionary inside the format() functions and access the values, again there are a few ways to use the dictionary<br><br>:#Issue the following:<source>
dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Province': 'ON'}
print('{}'.format(dict_york)) # Print out whole dictionary
print('{0}'.format(dict_york)) # Print out whole dictionary using format arguments position
print('{0[City]} {0[Country]}'.format(dict_york)) # Print out values using position and key {position[key]}
</source>:#With dictionaries however, instead of using positional arguments '''0''' each access to a key, python allows for expansion of keyword arguments. First <br><br>:#Let's take a look at the example of a keyword arguments, place the keyword variable name in between '''{}''' and add the keyword argument to the format() function:<source>
print('{string1} is {string2} {string2}'.format(string1='College', string2='Great!'))
</source>
:#Variables may also be passed to these keyword arguments:<source>
college = 'Seneca College'
print('{string1} is {string2} {string2}'.format(string1=college, string2='Great!'))
</source>:#Now back to dictionaries, using . Using keyword arguments(sometimes referred to as '''kwargs ''' in python). The dictionary can be quickly and easily expanded into these keyword arguments using the syntax '''**dictionary_name'''.<br><br>:#Let's try this below. Pay close attention to the keys inside the dictionary and the values associated with each key:<source>
dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Province': 'ON'}
print('{City} {Province} {Country}'.format(**dict_york)) # Uses the dictionary's keyword arguments
13,420
edits

Navigation menu