forked from UWPCE-PythonCert/IntroToPython-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_formatting.py
More file actions
50 lines (35 loc) · 1.28 KB
/
string_formatting.py
File metadata and controls
50 lines (35 loc) · 1.28 KB
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
#!/usr/bin/env python
"""
String formatting lab:
This version using "old style" formatting
"""
# Rewrite: "the first 3 numbers are: %i, %i, %i"%(1,2,3)
# for an arbitrary number of numbers...
# solution 1
# the goal was to demonstrate dynamic building of format strings:
# create the numbers
numbers = [32, 56, 34, 12, 48, 18]
# build the format string for the numbers:
formatter = " %i," * len(numbers)
formatter = formatter[:-1] # take the extra comma off the end
# put it together with the rest of the string
formatter = "the first %i numbers are: %s"%(len(numbers), formatter)
# use it:
# the format operator needs a tuple
# tuple(seq) will make a tuple out of any sequence
print formatter%tuple(numbers)
# solution 2
# in class, a couple people realized that str() would make a nice string from
# a list or tuple
numbers_str = str(numbers)[1:-1] # make a string, remove the brackets
# put it together with the rest of the string
print "the first %i numbers are: %s"%(len(numbers), numbers_str)
#####
# Write a format string that will take:
# ( 2, 123.4567, 10000)
# and produce:
# 'file_002 : 123.46, 1e+04'
#####
t = (2, 123.4567, 10000)
print "file_%03i : %10.2f, %.3g"%t
# could use '%e' for the last one, too -- I like '%g' -- it does significant figures...