1- ## String Formating : capital_abcd :
1+ ## String Formating
22
33### Basic of String Formatting
44- String Formatting is one good way to joins and combines two or more string with different data.
77
88### Using .format() we can archives different things
99
10- ### 1.String concatenation : spider_web :
10+ ### 1.String concatenation
1111Syntax
1212``` python
1313' {} {}' .format(list of variables)
1414```
1515
16- for example if we want to write ** "a value is 10"**
16+ for example, if we want to write ** " value is 10"**
1717
1818Normal way to write
1919``` python
2020a= 10
21- print (' a value is ' + str (a))
22- # Result:a value is 10
21+ print (' value is ' + str (a))
2322```
23+ Output:
24+ ```
25+ value is 10
26+ ```
27+
2428Using String Formatting
2529``` python
2630a= 10
27- print (' a value is {} ' .format(a))
28- # Result:a value is 10
31+ print (' value is {} ' .format(a))
2932```
33+ Output:
34+ ```
35+ value is 10
36+ ```
37+
3038if we want to print ** "a value is 10 and b value is 20 and c value is 39"**
3139
3240Normal way
3644c= 39
3745res= ' a value is ' + str (a)+ ' and b value is ' + str (b)+ ' and c value is ' + str (c)
3846print (res)
47+ ```
3948
40- # Result:a value is 10 and b value is 20 and c value is 39
49+ Output:
50+ ```
51+ a value is 10 and b value is 20 and c value is 39
4152```
4253
4354Using string Formatting
4657b= 20
4758c= 39
4859print (' a value is {} and b value is {} and c value is {} ' .format(a,b,c))
49- # Result:a value is 10 and b value is 20 and c value is 39
60+ ```
61+ Output:
62+ ```
63+ a value is 10 and b value is 20 and c value is 39
5064```
5165
5266- You can also give position inside {} block like {1} then this will get value from ** .format(0position,1position,2postion)**
5569b= 20
5670c= 39
5771print (' a value is {2} and b value is {1} and cvalue is {0} ' .format(a,b,c))
58- # Result:a value is 39 and b value is 20 and cvalue is 10
59-
6072```
61- - here ** {2}** get value from format and get value whose position is 2 in formatting.
73+ Output:
74+ ```
75+ a value is 39 and b value is 20 and cvalue is 10
76+ ```
77+ - here ** {2}** get value from the format and get value whose position is 2 in formatting.
6278
6379- We can also use variable name inside a braces also as following
6480``` python
6783c= 39
6884v= ' a value is {a} and b value is {b} and cvalue is {c} ' .format(a = 10 ,b = 20 ,c = 39 )
6985print (v)
70-
71- # Result:a value is 10 and b value is 20 and cvalue is 39
86+ ```
87+ Output:
88+ ```
89+ a value is 10 and b value is 20 and cvalue is 39
7290```
7391
7492
75- ### 2.Type Conversion : shinto_shrine :
93+ ### 2.Type Conversion
7694- Type conversion int to float
7795
78- following example showing conversion of int datatype to float datatype.
96+ The following example showing the conversion of int datatype to float datatype.
7997
8098Example
8199``` python
82100a= 10
83101print (' a value is {0:f } ' .format(a))
84- # Result:a value is 10.000000
102+ ```
103+ Output:
104+ ```
105+ a value is 10.000000
85106```
86107conversion int datatype to float datatype with ** 2** precision point.
87108
88109Example
89110``` python
90111a= 10
91112print (' a value is {0:.2f } ' .format(a))
92- # Result:a value is 10.00
113+ ```
114+ Output:
115+ ```
116+ a value is 10.00
93117```
94118- type conversion double to float
95119- while calulation in python 3 the defualt return type of calculation is ** double**
96120
97121Example
98122``` python
99123print (' 2/3 answer is {} ' .format(2 / 3 ))
100- # Result:2/3 answer is 0.6666666666666666
124+ ```
125+
126+ Output:
127+ ```
128+ 2/3 answer is 0.6666666666666666
101129
102130```
103131- To conversion of double daatatype to float datatpe.
104132Example:
105133``` python
106134print (' 2/3 answer is {0:f } ' .format(2 / 3 ))
107- # Result:2/3 answer is 0.666667
135+ ```
136+ Output:
137+ ```
138+ 2/3 answer is 0.666667
108139```
109140
110-
111- ### 3.Space around the word :part_alternation_mark :
141+ ### 3.Space around the word
112142
113143- This is used for giving spacing for integer variables
114144``` python
115145print (' answer is {0:3d } ' .format(2 ))
116-
117- # Result:answer is 2
118146```
147+ Output:
148+ ```
149+ answer is 2
150+ ```
151+
119152here the given element is length of 1 digit and we give space for 3 digit means only 2 digit space will displayed.
120153
121154
@@ -124,27 +157,40 @@ here the given element is length of 1 digit and we give space for 3 digit means
124157Example:
125158``` python
126159print (' my name is {} ' .format(' abhi' ))
127- # Result:my name is abhi
160+ ```
161+ Output:
162+ ```
163+ my name is abhi
128164```
129165
130166- To give space before name we use right chevron
131167``` python
132168print (' my name is {0:>8 } ' .format(' abhi' ))
133- # Result:my name is abhi
169+ ```
170+ Output:
171+ ```
172+ my name is abhi
134173```
135174
136175- To give space after name we use right chevron
137176``` python
138177print (' my name is {0:<8 } ....' .format(' abhi' ))
139- # Result:my name is abhi ....
140178```
141- ### 4.Asterisk :eight_pointed_black_star : around the text
179+
180+ Output:
181+ ```
182+ my name is abhi ....
183+ ```
184+ ### 4.Asterisk around the text
142185Example:
143186
144187``` python
145188print (' {0: *^11s} ' .format(' rahul' ))
146- # Result:***rahul***
189+ ```
147190
191+ Output:
192+ ```
193+ :***rahul***
148194```
149195This will create 11 * but ** rahul** string is have 5 length characters so 6 asterisk(* ) will be printed at 3 left side and 3 right side.
150196
@@ -153,18 +199,23 @@ printing start from right side to left.
153199Example:
154200``` python
155201print (' {0: *^12s} ' .format(' rahul' ))
156- # Result:***rahul****
157202```
158203
159- ### 5.String Formatting inside a loop :loop :
204+ Output:
205+ ```
206+ ***rahul****
207+ ```
160208
209+ ### 5.String Formatting inside a loop
161210Example
162211``` python
163212for i in range (1 ,11 ):
164- print (' {0:5d }{1:5d }{2:5d } ' .format(i,i** 2 ,i** 3 ))
165- '''
166- #Result:
167- 1 1 1
213+ print (' {0:5d }{1:5d }{2:5d } ' .format(i,i** 2 ,i** 3 ))
214+ ```
215+
216+ Output:
217+ ```
218+ ``` 1 1 1
168219 2 4 8
169220 3 9 27
170221 4 16 64
@@ -174,5 +225,4 @@ for i in range(1,11):
174225 8 64 512
175226 9 81 729
176227 10 100 1000
177- '''
178- ```
228+ ```
0 commit comments