forked from alanhou/python-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_formatting.py
More file actions
49 lines (39 loc) · 903 Bytes
/
string_formatting.py
File metadata and controls
49 lines (39 loc) · 903 Bytes
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
# Basic formatting
a = 10
b = 20
print("The values of a and b are %d %d" % (a, b))
c = a + b
print("The value of c is %d" % c)
str1 = "John"
print("My name is %s" % str1)
x = 10.5
y = 33.5
z = x * y
print("The value of z is %f" % z)
print()
# aligning
name = "Mary"
a = 10
b = 20
print("The values of a and b are %d %d" % (a, b))
c = a + b
print("The value of c is %d" % c)
str1 = "John"
print("My name is %s" % str1)
x = 10.5
b = 33.5
z = x * y
print("The value of z is %f" % z)
print()
# aligning
name = "Mary"
print("Normal: Hello, I am %s !!" % name)
print("Right aligned: Hello, I am %10s !!" % name)
print("Left aligned: Hello, I am %-10s !!" % name)
print()
# truncating
print("The truncated string is %.4s" % ("Examination"))
print()
# formatting placeholders
students = {'Name' : 'John', 'Address' : 'New York'}
print("Student details: Name:%(Name)s Address:%(Address)s" % students)