-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit1_notes_string_methods.py
113 lines (95 loc) · 3.58 KB
/
unit1_notes_string_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Name: Mr. Callaghan
Title: String methods & augmented assignment operators
Description: Running methods on strings and better ways to write operators.
"""
# OBJECT - an entity in a program that can be manipulted using methods (String in this case)
# METHOD - procedure you are calling on an object to manipulate it (parentheses are required)
# STRING METHODS - methods that allows us to obtain new string values from existing string values
# DOT NOTATION - <object>.<method>(<potential arguments>)
name = "Monty python"
print(name)
name_upper = name.upper()
print(name_upper)
name_lower = name.lower()
print(name_lower)
name_title = name.title()
print(name_title)
name_swap = name.swapcase()
print(name_swap)
name_replace = name.replace("o", "i")
print(name_replace)
print("Original name:", name)
# name remains its original value; methods return new strings rather than changing the exisiting string
# RETURN VALUE - value received back after calling a function or method.
''' OPTIONAL TO SHOW IN SHELL
Type in shell:
>>> name = "Monty python"
>>> name
'Monty python'
>>> name.upper()
'MONTY PYTHON'
>>> name.lower()
'monty python'
>>> name.title()
'Monty Python'
>>> name.swapcase()
'mONTY PYTHON'
>>> name.replace("o", "i")
'Minty pythin'
>>> name
'Monty python' # name remains its original value; methods return new strings rather than changing the exisiting string
>>> name = name.upper()
>>> name
'MONTY PYTHON'
'''
# handling user input with string methods
name = input("What is your name: ").title() # chaining methods can save some typing
#name.title() # must assing the new string!
# name = name.title()
print(name)
# !!! Instructor note: DO PEER INSTRUCTION #4
# Python Documentation for The Python Standard Library (python.org) is the official reference for language features.
# Google 'python docs' followed by the documentation you are looking for. (e.g. "Python docs String")
# STRING FORMATTING - .format() method and f-strings improves the ability to work with strings
# and other data types. Python has fill-in-the-braces format, similar to
# fill-in-the-blank questions.
# INPUTS
origPrice = float(input('Enter the original price: $'))
discount = float(input('Enter discount percentage: '))
# !!! Fill in the rest and have students try to figure out the formula for this calculation in pairs on scratch paper.
# !!! Suggest working with sample values (e.g. $100 discounted by 20% is $80)
# CALCULATIONS
newPrice = (1 - discount/100) * origPrice
# OUTPUT
# Formatting currency in Python
# , is thousands separator; .2f stands for two decimal float
# The .format() string method has been upgraded in recent versions of Python with f-strings.
calculation = f'${origPrice:,.2f} discounted by {discount}% is ${newPrice:,.2f}.'
print(calculation)
print(f"\nf-strings can even evaluate expressions!: {10 * 10}")
# Python has certain built-in functions which are always available (google "Python docs built-in functions")
# e.g. print, input, int, float, str, round, and abs can be used in any program
num = -3.3333333333333333333
num = round(num, 2) # round function - rounds float to a specified number of decimal places
num = abs(num) # absolute value function - returns absolute value of a number
print(num)
# AUGMENTED ASSIGNMENT OPERATORS - shortcut ways to combine math operators
# and assignment operator
total = 0
total = total + 10
print (total)
print()
total += 10 # same as total = total + 10
print (total)
total *= 10
print (total)
total -= 10
print(total)
total /= 10
print(total)
total //= 10
print(total)
total %= 10
print(total)
# !!! Instructor note: DO PEER INSTRUCTION #5