-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Formatting_Examples.py
More file actions
294 lines (210 loc) · 6.4 KB
/
String_Formatting_Examples.py
File metadata and controls
294 lines (210 loc) · 6.4 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
__author__='jcbrent'
# Str function
print(' String functions')
print(' ------------------------------------------------------------------------------ ')
message = 'Hello everyone'
print(message)
print(len(message))
print(' Slicing')
print(message[10])
# Print a range of chars
print(message[0:5])
#Starts at 0 : thru to char 5
print(message[7:]) # will print from the 7th char to the end.
print(message[:7]) # will print from the beggining to the 7th char.
# a method is a function that belongs to an object.
print(message.lower())
print(message.upper())
# counting
print(message.count('l'))
print(message.count('Hello'))
# Find the location of an item
print(message.find('Hello'))
print(message.find('y'))
print(message.find('universe')) # will return -1 because it doesn't exist in the var - message
print('')
# replacements.
message.replace('Everyone', 'anyone')
print(message)
# must set a new var to replace the text
new_message = message.replace('everyone', 'anyone')
print(new_message)
# or you set the old var equal to the modified var
message = message.replace('everyone', 'anyone')
print(message)
print('')
print('Concatenating Strings')
greeting = 'Hello'
name = 'Joe'
concat_message = greeting + ' ' + name + '. Welcome.'
print(concat_message)
print('')
message_f_string = f'{greeting}, {name}. Welcome!'
print(message_f_string)
print('')
new_f_string_message = f'{greeting}, {name.upper()}. Welcome!'
print(new_f_string_message)
print('')
# method list
print(dir(name))
print('')
# get help
# print(help(str)) # Too verbose.
print('')
# get help on a specific function/method
print(help(str.lower)) # Too verbose.
print('')
print('')
# print('Hello World')
# print (1 + 2)
# print (7 * 6)
# print ('The End')
# print ("Pythons string are easy to use")
# print ('We can even include "quotes" in string')
# print ("hello" + " world")
#
# greeting = "Hello"
# name = "Bruce"
# print (greeting + name)
# # comments
# print(greeting + ' ' + name)
# greeting = ("Hello")
# name = input("Please enter your name ")
# print(greeting + ' ' + name)
# splitString = "This string has been \nsplit over\n several lines"
# print (splitString)
#
# tabbedString = "1\t2\t\3\t4\t5\t"
# print (tabbedString)
print ('the pet shop owner said "No, no \'e\'s uh... he\'s resting"') #preferred method
print ("the pet shop owner said \"No, no 'e's uh... he\sting\"")
anotherSplitString = """ The pet shop owner
split
the string
over several lines."""
print (anotherSplitString)
print (''' The pet shop owner said "No, no, 'e's uh, ... he's resting"''')
print('')
age = 100
# NOT Correct --- print("My age is " + age + "years")
# must use str method - a built in method
print("My age is " + str(age) + " years.")
# String Formatting with the f function is implicit interpolation
# implicitly do by using the name of the function to convert
decimal_example = 12.5437
print(decimal_example)
int_convert = int(decimal_example)
print(int_convert)
my_list = (1,2,3)
print(my_list)
print('That was a list')
print('This is a string')
my_list_as_a_string = str(my_list)
print(my_list_as_a_string)
print('')
num = 12
print(num)
print(type(num))
print('')
print('Change to float: num = float(num)')
num = float(num)
print(num)
print(type(num))
print('')
print('')
print('Division changes ints to floats implicitly.')
print('1/3')
print(1/3)
print('')
print('Changing a float to an int just chops off the decimals.')
print('int(99.999')
print(int(99.999))
print('')
print('String function = str')
num = 12
print(num)
print(type(num))
print(' str(num)')
print(str(num))
print(type(num))
print(str(num) + ' is now a string')
# Formatting Strings
print(' String formatting: F method. ~ Python 3.6 method.')
print(' ------------------------------------------------------------------------------ ')
print('')
print('Note: % operator now deprecated = pre 3.5 method')
print('')
print('')
# F Strings Python 3.6 New method.........
# Take the value in the {} and turns it into a string and substitutes it into the {}
x = 10
formatted = f"I've told you {x} times already!"
print(formatted)
print(f"I've told you {x + 1 } times already!")
print('')
y = 11
formatted2 = f"First Var is {x} and the second var is {y}"
print(formatted2)
print('formatted2 = f"First Var is {x} and the second var is {y}"')
print('')
print(' ------------------------------------------------------------------------------ ')
print(' String formatting: Format method. ~ Python 3.5 and before method.')
print(' ------------------------------------------------------------------------------ ')
formatted_old = "I've told you {} times already!".format(12)
print(formatted_old)
# Example
first = "Jim"
last = "B."
formatted = ("{} {}".format(first, last))
print(formatted)
formatted = ("first name: {}, last name: {}".format(first, last))
print(formatted)
print(' ------------------------------------------------------------------------------ ')
print(' ')
print('Replacement fields python3.5')
print(' ---------------------')
print("My age is {0} years".format(age))
print("There are {0} days in {1}, {2}, {3}, {4}, {5}, {6} and {7} ".format(31, "January", "March", "May", "July", "August", "October", "December"))
print(' ')
print("""January: {2}
February: {0}
March: {2}
April: {1}
May: {2}
June: {1}
July: {2}
August: {2}
September: {1}
October: {2}
November: {1}
December: {2}""".format(28,30,31))
print(' ')
print('Python 2 deprecated formatting operator')
print(' ---------------------')
print("My age is %d years" % age)
print("My age is %d %s, %d %s" % (age, "years", 6, "months"))
print(' ')
for i in range(1,12):
print("No. %2d squared is %4d and cubed is %4d" %(i, i ** 2, i ** 3))
print("Pi is approximately %12f" % ( 22 / 7 ))
print(' ')
print("Pi is approximately %12.50f" % ( 22 / 7 ))
print("50 places = precision")
print('')
print('Replacement syntax')
print("Python3.5 replacement fields formatting of above")
print(' ---------------------')
#Right formatting
for i in range(1,12):
print("No. {0:2} squared is {1:4} and cubed is {2:4}".format(i, i ** 2, i ** 3))
#Left formatting
for i in range(1,12):
print("No. {0:2} squared is {1:<4} and cubed is {2:<4}".format(i, i ** 2, i ** 3))
print(' ---------------------')
print("Pi is approximately {0:12.50f}".format( 22 / 7 ))
print("50 places = precision")
print(' ')
print('For loop using no params in array, just in order')
for i in range(1,12):
print("No. {} squared is {:4} and cubed is {:4}".format(i, i ** 2, i ** 3))
#