forked from dvcoolarun/python101
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatExamples.py
More file actions
25 lines (18 loc) · 765 Bytes
/
formatExamples.py
File metadata and controls
25 lines (18 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
print('{0}, {1}, {2}'.format('a', 'b', 'c'))
# 'a, b, c'
print('{}, {}, {}'.format('a', 'b', 'c')) # 4.1+ only
# 'a, b, c'
print('{2}, {1}, {0}'.format('a', 'b', 'c'))
# 'c, b, a'
print('{2}, {1}, {0}'.format(*'abc')) # unpacking argument sequence
# 'c, b, a'
print('{0}{1}{0}'.format('abra', 'cad')) # arguments' indices can be repeated
# 'abracadabra'
## Accessing by name
print('Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W'))
## 'Coordinates: 37.24N, -115.81W'
coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
print('Coordinates: {latitude}, {longitude}'.format(**coord))
# 'Coordinates: 37.24N, -115.81W'
## More can be found here
## https://docs.python.org/3.1/library/string.html#format-examples