Skip to content

Commit 9c2c3ce

Browse files
committed
String Formatting
space using 'd', text at center of SpecialSymbol, String formating inside a loop
1 parent 90fb124 commit 9c2c3ce

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

String/4.StringFromatting.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#String Formatting is one good way to joins and combines two or more string with diffrent data
2+
#for string formatting {} is used to blank space to get data from .format() .format(variabless are used)
3+
4+
a=10
5+
b=20
6+
c=39
7+
8+
#1.String Formatting
9+
#for example if we want to write a value is 10 then normal way to write is
10+
print('a value is '+str(a))
11+
12+
13+
#using String Formatting
14+
print('a value is {}'.format(a))
15+
16+
17+
#if we want to print a value is 10 and b value is 20 and c value is 39
18+
#normal way
19+
print('a value is '+str(a)+' and b value is '+str(b)+' and c value is '+str(c))
20+
21+
#using string Formatting
22+
print('a value is {} and b value is {} and c value is {}'.format(a,b,c))
23+
24+
25+
#You can also give position inside {} block like {1} then this will get value from .format(0position,1position,2postion)
26+
print('a value is {2} and b value is {1} and cvalue is {0}'.format(a,b,c))
27+
#here {2} get value from format and get value whose position is 2 in formatting
28+
29+
30+
31+
#2.Type Conversion
32+
33+
#Type conversion int to float
34+
#foolowing example showing int to float
35+
print('a value is {0:f}'.format(a))
36+
37+
38+
#for 2 digit
39+
print('a value is {0:.2f}'.format(a))
40+
41+
42+
43+
#type conversion double to flot
44+
#bydefault in python the dataatype is double for calculation
45+
print('2/3 answer is {}'.format(2/3))
46+
47+
#to convert into float
48+
print('2/3 answer is {0:f}'.format(2/3))
49+
50+
51+
52+
53+
#3.#spacing using 'd'
54+
#this is used for giving spacing to integer
55+
print('answer is {0:3d}'.format(2))
56+
#here the element is 1 digit and we give space for 3 digit means only 2 digit space show
57+
58+
59+
#for string we use left chevorn(<) and right chevorn(>) for giving space
60+
print('my name is {}'.format('abhi'))
61+
62+
#to give space before name we use right chevorn
63+
print('my name is {0:>8}'.format('abhi'))
64+
65+
66+
#to give space after name we use right chevorn
67+
print('my name is {0:<8} ....'.format('abhi'))
68+
69+
70+
71+
#4.text at center of Astric (*)
72+
print('{0:*^11s}'.format('rahul'))
73+
74+
#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
75+
76+
77+
78+
#5.String Formatting inside a loop
79+
for i in range(1,11):
80+
print('{0:5d}{1:5d}{2:5d}'.format(i,i**2,i**3))

0 commit comments

Comments
 (0)