Skip to content

Commit 1168e5e

Browse files
author
anastomose
committed
Added string printing lab, updated notes
1 parent ab4c99d commit 1168e5e

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎Students/ebuer/session_03/s3_notefile.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Session 03 notes from class
22
---------------------------------
3-
String
3+
Strings
44
ord () is the ordinal of a string value. This allows us to quickly alphabetize strings. Cap letters are indexed in front of lower case.
55

66
string.index() locates the first instance of a specified string passed to the operator.
@@ -24,6 +24,8 @@ continue -- skips everything below the continue line and returns to the top of t
2424

2525
else on a "FOR" loop -- else is executed if the loop does not break, e.g. it runs to completion.
2626

27+
enumerate(argument) -- returns both the index and the value for an argument
28+
2729
string.split(argument) -- splits string on argument passed
2830
string.join(argument) -- join list values into a string with argument separators
2931

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#String formatting lab
2+
3+
#create a function that prints in integer format for
4+
#an arbitary number of ints or other values
5+
test_list = [1, 3, 5, 7, 13, 17, 23, 29.5]
6+
7+
def intprinter(L=[0,1,2,3]):
8+
n = len(L)
9+
ni = []
10+
for i in L:
11+
ni.append(int(i))
12+
temp=str(ni).lstrip('[').rstrip(']')
13+
print 'The first %i numbers are %s' %(n, temp)
14+
15+
intprinter(test_list)
16+
17+
18+
#write a formatting string to transform the provided inputs
19+
lab_inp = (2, 123.4567, 10000) #it's a tuple!
20+
21+
#using old formatting
22+
print 'file_00%i, %.2f, %.3e' %(lab_inp[0], lab_inp[1], lab_inp[2])
23+
24+
#using .format
25+
newstring = 'file_00{a:d}, {b:.2f}, {c:.0e}'\
26+
.format(a=lab_inp[0], b = lab_inp[1], c = lab_inp[2] )
27+
28+
print 'your formatted string: %s' %newstring
29+

0 commit comments

Comments
 (0)