forked from chavarera/PythonScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.StringFromatting.py
More file actions
80 lines (45 loc) · 2.12 KB
/
4.StringFromatting.py
File metadata and controls
80 lines (45 loc) · 2.12 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#String Formatting is one good way to joins and combines two or more string with diffrent data
#for string formatting {} is used to blank space to get data from .format() .format(variabless are used)
a=10
b=20
c=39
#1.String Formatting
#for example if we want to write a value is 10 then normal way to write is
print('a value is '+str(a))
#using String Formatting
print('a value is {}'.format(a))
#if we want to print a value is 10 and b value is 20 and c value is 39
#normal way
print('a value is '+str(a)+' and b value is '+str(b)+' and c value is '+str(c))
#using string Formatting
print('a value is {} and b value is {} and c value is {}'.format(a,b,c))
#You can also give position inside {} block like {1} then this will get value from .format(0position,1position,2postion)
print('a value is {2} and b value is {1} and cvalue is {0}'.format(a,b,c))
#here {2} get value from format and get value whose position is 2 in formatting
#2.Type Conversion
#Type conversion int to float
#foolowing example showing int to float
print('a value is {0:f}'.format(a))
#for 2 digit
print('a value is {0:.2f}'.format(a))
#type conversion double to flot
#bydefault in python the dataatype is double for calculation
print('2/3 answer is {}'.format(2/3))
#to convert into float
print('2/3 answer is {0:f}'.format(2/3))
#3.#spacing using 'd'
#this is used for giving spacing to integer
print('answer is {0:3d}'.format(2))
#here the element is 1 digit and we give space for 3 digit means only 2 digit space show
#for string we use left chevorn(<) and right chevorn(>) for giving space
print('my name is {}'.format('abhi'))
#to give space before name we use right chevorn
print('my name is {0:>8}'.format('abhi'))
#to give space after name we use right chevorn
print('my name is {0:<8} ....'.format('abhi'))
#4.text at center of Astric (*)
print('{0:*^11s}'.format('rahul'))
#this will create 11 * but rahul string is 5 chacter so 6 astric(*) will be printed 3 left side and 3 right side if 7 star there then 4 at right side and 3 at left side
#5.String Formatting inside a loop
for i in range(1,11):
print('{0:5d}{1:5d}{2:5d}'.format(i,i**2,i**3))