-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_methods.py
More file actions
299 lines (224 loc) · 8.88 KB
/
string_methods.py
File metadata and controls
299 lines (224 loc) · 8.88 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
294
295
296
297
298
299
# The capitalize() method returns a string where the first character is upper case, and the rest
# are lower case.
txt = "hello, and welcome to my world."
x = txt.capitalize()
print (x)
txt = "python is FUN!"
x = txt.capitalize()
print (x)
txt = "36 is my age."
x = txt.capitalize()
print (x)
#casefold() method is used for caseless matching. It is similar to lower() but more aggressive
# because it is intended to remove all case distinctions in a string.
txt = "Hello, And Welcome To My World!"
x = txt.casefold()
print(x)
#center() method will center align the string, using a specified character (space is default) as
# the fill character.
txt = "banana"
x = txt.center(50)
print(x)
txt = "banana"
x = txt.center(20, "*")
print(x)
#count() method returns the number of times a specified value occurs in a string.
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
x = txt.count("apple", 10, 24)
print(x)
# The encode() method encodes the string, using the specified encoding. If no encoding is specified,
# UTF-8 will be used.
txt = "My name is Ståle"
x = txt.encode()
print(x)
print(txt.encode(encoding="ascii",errors="backslashreplace"))
print(txt.encode(encoding="ascii",errors="ignore"))
print(txt.encode(encoding="ascii",errors="namereplace"))
print(txt.encode(encoding="ascii",errors="replace"))
print(txt.encode(encoding="ascii",errors="xmlcharrefreplace"))
# The endswith() method returns True if the string ends with the specified value, otherwise False.
txt = "Hello, welcome to my world."
x = txt.endswith("y")
print(x)
x = txt.endswith("my world.", 5, 11)
print(x)
txt = "Hello, welcome to my castle."
x = txt.endswith(("world.", "castle."))
print(x)
# The expandtabs() method sets the tab size to the specified number of whitespaces.
txt = "H\te\tl\tl\to"
print(txt)
print(txt.expandtabs())
print(txt.expandtabs(2))
print(txt.expandtabs(4))
print(txt.expandtabs(10))
# The find() method finds the first occurrence of the specified value. The find() method returns -1
# if the value is not found.
txt = "Hello, welcome to my world."
x = txt.find("e")
print(x)
x = txt.find("e", 5, 10)
print(x)
print(txt.find("q"))
#print(txt.index("q"))
# The format() method formats the specified value(s) and insert them inside the string's placeholder.
# The placeholder is defined using curly brackets: {}.
txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))
txt = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)
print(txt)
txt = "My name is {0}, I'm {1}".format("John",36)
print(txt)
# The format_map() method is similar to format(), but it takes a dictionary as an argument,
# where the keys are the placeholders in the string.
my_dict = {'fname': 'Johnny', 'age': 36}
txt = "My name is {fname}, I'm {age}".format_map(my_dict)
print(txt)
# The index() method finds the first occurrence of the specified value. The index() method raises an
# exception if the value is not found.
txt = "Hello, welcome to my world."
x = txt.index("d")
print(x)
x = txt.index("e", 5, 10)
print(x)
print(txt.find("q"))
#print(txt.index("q"))
# The isalnum() method returns True if all characters in the string are alphanumeric (either
# alphabets or numbers), and there is at least one character, otherwise False.
txt = "CompanyX%"
x = txt.isalnum()
print(x)
# The isalpha() method returns True if all characters in the string are in the alphabet, and there
# is at least one character, otherwise False.
txt = "CompanyX12"
x = txt.isalpha()
print(x)
# The isdecimal() method returns True if all characters in the string are decimals, and there is at
# least one character, otherwise False.
a = "\u0030" #unicode for 0
b = "\u0047" #unicode for G
print("is decimal:",a.isdecimal())
print("is decimal:",b.isdecimal())
# The isdigit() method returns True if all characters in the string are digits, and there is at
# least one character, otherwise False
a = "\u0037" #unicode for 7
b = "\u0047" #unicode for G
print("is digit:",a.isdigit())
print("is digit:",b.isdigit())
# The isidentifier() method returns True if the string is a valid identifier, otherwise False.
# A string is considered a valid identifier if it only contains alphanumeric characters
# (A-Z, a-z, 0-9) or underscores (_), and it does not start with a number. Python keywords cannot
# be used as identifiers, even though they are valid identifiers.
txt = "123_Demo_123"
x = txt.isidentifier()
print("is identifier:",x)
txt = "DemoText"
x = txt.isidentifier()
print("is identifier1:",x)
a = "MyFolder"
b = "Demo002"
c = "2bring"
d = "my demo"
print("is identifier2:",a.isidentifier())
print("is identifier3:",b.isidentifier())
print("is identifier4:",c.isidentifier())
print("is identifier5:",d.isidentifier())
# The islower() method returns True if all characters in the string are lower case, and there is at
# least one character, otherwise False.
txt = "hello worlD!"
x = txt.islower()
print("is lower:",x)
# The isnumeric() method returns True if all characters in the string are numeric, and there is
# at least one character, otherwise False.
a = "\u0034" #unicode for 4
b = "\u0047" #unicode for G
print("is numeric:",a.isnumeric())
print("is numeric:",b.isnumeric())
# The isprintable() method returns True if all characters in the string are printable, otherwise False.
txt = "Hello! Are you #1?"
x = txt.isprintable()
print("is printable:",x)
# The isspace() method returns True if all characters in the string are whitespaces, and there is at
# least one character, otherwise False.
txt = " space "
x = txt.isspace()
print("is space:",x)
# The istitle() method returns True if the string follows the rules of a title, where the first
# letter of each word is upper case, and the rest are lower case, otherwise False.
txt = "Hello, And Welcome To My World!"
print("is title:",txt.istitle())
# The isupper() method returns True if all characters in the string are upper case, and there is at
# least one character, otherwise False.
txt = "THIS IS hOW!"
print("is upper:",txt.isupper())
# The join() method takes all items in an iterable and joins them into one string. A string must be
# specified as the separator.
myTuple = ("John", "Peter", "Vicky")
x = "#".join(myTuple)
print(x)
myDict = {"name": "John", "country": "Norway"}
mySeparator = "TEST"
x = mySeparator.join(myDict)
print(x)
# The ljust() method returns a left justified version of the string, using a specified character
# (space is default) as the fill character.
txt = "banana"
x = txt.ljust(20)
print(x, "is my favorite fruit.")
txt = "banana"
x = txt.ljust(20, "*")
print(x, "is my favorite fruit.")
# The lower() method returns a string where all characters are lower case.
txt = "Hello my FRIENDS"
x = txt.lower()
print(x)
# The lstrip() method returns a left trim version of the string. The lstrip() method removes any
# leading characters (space is the default leading character to remove).
txt = " banana "
x = txt.lstrip()
print("of all fruits", x, "is my favorite")
txt = ",,,,,ssaaww.....banana"
x = txt.lstrip(",.asw")
print(x)
# The maketrans() method returns a translation table to be used in translations. The translate() method
# takes a translation table and translates the string according to that table.
txt = "Hello Sam!"
mytable = txt.maketrans("S", "P")
print(txt.translate(mytable))
txt = "Good night Sam!"
x = "mSa"
y = "eJo"
z = "odnght"
mytable = str.maketrans(x, y, z)
print(txt.translate(mytable))
print(str.maketrans(x, y, z))
# The partition() method searches for a specified string, and splits the string into a tuple
# containing three elements: the part before the specified string, the specified string itself, and
# the part after the string.
txt = "I could eat bananas all day"
x = txt.partition("bananas")
print(x)
txt = "I could eat bananas all day"
x = txt.partition("apples")
print(x)
# The replace() method replaces a specified phrase with another specified phrase.
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x)
txt = "one one was a race horse, two two was one too."
x = txt.replace("one", "three",2)
print(x)
# The rfind() method finds the last occurrence of the specified value. The rfind() method returns -1 if the value is not found.
txt = "Mi casa, su casa."
x = txt.rfind("casa")
print(x)
x = txt.rfind("casa", 0, 10)
print(x)
# The rindex() method finds the last occurrence of the specified value. The rindex() method raises an exception if the value is not found.
txt = "Mi casa, su casa."
x = txt.rindex("casa")
print(x)
x = txt.rindex("casa", 0, 10)
print(x)