forked from UWPCE-PythonCert/IntroPython-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_formatting.py
More file actions
57 lines (40 loc) · 1.61 KB
/
string_formatting.py
File metadata and controls
57 lines (40 loc) · 1.61 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
50
51
52
53
54
55
56
#!/usr/bin/env python
"""
String formatting lab:
This version using the format() method
"""
#####
# Write a format string that will take the tuple:
# (2, 123.4567, 10000, 12345.67)
# and produce:
# 'file_002 : 123.46, 1.00e+04, 1.23e+04'
#####
print("file_{:03d} : {:10.2f}, {:.2e}, {:.3g}".format(2, 123.4567, 10000, 12345.67))
print()
# Note the subtle differnce between the 'e' and 'g' formatting strings.
# I like 'g' -- it does significant figures.
#######################
# Rewrite: "the 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:
def formatter(t):
# The static part of the string
fstring = "the {:d} numbers are: ".format(len(t))
# This add the correct number of format specifiers:
fstring += ", ".join(['{:d}'] * len(t))
# The created string can be now applied to the tuple of numbers
# * unpacks a sequence into the arguments of a function -- we'll get to that!
return fstring.format(*t)
# call it with a couple different tuples of numbers:
print(formatter((2, 3, 5)))
print(formatter((2, 3, 5, 7, 9)))
# solution 2
# You may have realized that str() would make a nice string from
# a list or tuple
# perfectly OK to use that -- though it doesn't demonstrate how you can
# dynamically build up format strings, and then use them later...
numbers = (34, 12, 3, 56)
numbers_str = str(numbers)[1:-1] # make a string, remove the brackets
# put it together with the rest of the string
print("the first {:d} numbers are: {}".format(len(numbers), numbers_str))