-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGUIDE_string.py
More file actions
195 lines (133 loc) · 4.4 KB
/
GUIDE_string.py
File metadata and controls
195 lines (133 loc) · 4.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
# author: Asmaa Mirkhan ~ 2019
# STRINGS
# -----------------------------------
# string declaration
sample_string = 'asmaa'
hello_string = "hello world"
multi_line = """Hello,
my name is asmaa"""
print(sample_string, hello_string)
print(multi_line)
# Output: asmaa hello world
# Hello,
# my name is asmaa
# -----------------------------------
# access chars (indexing) and slicing
print('Accessing and slicing: ',
hello_string[0], hello_string[1:3], hello_string[:5], hello_string[-1])
# Output: h el hello d
# Note: string are immutable
# hello_string[1] = 'f' # ERROR
# -----------------------------------
# Arithmetic operators on strings
my_string = hello_string + ' ' + sample_string
print('+ op: ', my_string)
my_string = hello_string * 3
print('* op: ', my_string)
my_string = 'hello''world'
print(my_string)
# Output:
# hello world asmaa
# hello worldhello worldhello world
# helloworld
# -----------------------------------
# iterating enumarate(sttring)
for i, letter in enumerate(my_string):
print(i, letter)
# Output: prints all chars in the string with indeces
# -----------------------------------
# membership test
print('Membership: ', 'h' in my_string, 'm' in my_string)
# Output: True Flase
# -----------------------------------
# string formatting
# %s - String (or any object with a string representation, like numbers)
# %d - Integers
# %f - Floating point numbers
age, name, floatnum = 5, 'asmaa', 1.12345
mystr = 'My name is %s and I am %d years old, %.2f' % (name, age, floatnum)
print(mystr)
# Output: My name is asmaa and I am 5 years old, 1.12
mystr1 = 'My name is {} and I am {} years old, {:.3f}'.format(name, age, floatnum)
print(mystr1)
# Output: My name is asmaa and I am 5 years old, 1.123
mystr2 = 'My name is ' + name + ' and I am ' + str(age) + ' years old, ' + str(floatnum)
print(mystr2)
# Output: My name is asmaa and I am 5 years old, 1.12345
# -----------------------------------
# char operations:
print('Upper, lower:', my_string.upper(), my_string.lower())
# Output:
# NAME: ASMAA, SURNAME: MIRKHAN name: asmaa, surname: mirkhan
# swapcase(string) case swapping :P
string = 'AsmAA MirKhaN'
print('Case swapping:', string.swapcase())
# Output: aSMaa mIRkHAn
# .title(): returns camel case string with spaces
s = 'introduction to python'
print('Title:', s.title())
# Output: Introduction To Python
# -----------------------------------
# find(string) returns index of first matching substring
print('Find find():', my_string.find('me'))
# rfind(string) returns index of last matching substring
print('Find rfind():', my_string.rfind('me'))
# Output:
# 2
# 18
# -----------------------------------
# splitting:
splitted = my_string.split()
print('Splitting:', splitted)
# joining
joined = ''.join(splitted)
print('Joining', joined)
# Output:
# ['name:', 'asmaa,', 'surname:', 'mirkhan']
# name:asmaa,surname:mirkhan
# another example
# declare a string
original = 'My name is Asmaa'
# convert string into list and splitting criteria is ' ' (space)
split = original.split(' ')
print('Splitting:', split)
# rejoin string (convert list into string again) and
# separate words by ',' ==> this parameter can be any string
join = ','.join(split)
print('Joining:', join)
# Output:
# ['My', 'name', 'is', 'Asmaa']
# My,name,is,Asmaa
# -----------------------------------
# replacing:
my_string = my_string.replace('asmaa', 'esma')
print('Replacing:', my_string)
# Output: name: esma, surname: mirkhan
# -----------------------------------
# filling and centering
my_string = my_string.center(40, '*')
print('Filling: ', my_string)
# Output: ******name: esma, surname: mirkhan******
# -----------------------------------
# insensitive copmaring
print('Casefold:', 'asmaa'.casefold() == 'ASmaa'.casefold())
# Output: True
# -----------------------------------
# start control
print('Start control:', 'asmaa'.startswith('aa'), 'asmaa'.startswith('asm'))
# Output: False True
# start control with parameter (membership test again :/)
# index parameters are a range [start, end)
print('Start control with parameter:', 'asmaa'.startswith('sm', 1, 3))
# Output: True
# end control
print('End control:', 'asmaa'.endswith('aa'))
# Output: True
# -----------------------------------
# char type control
print('Char control: ', 'aa11ss22'.isalnum(), '--sssaaa2222'.isalnum())
print('Char control:', '10.15'.isdecimal(),
'15'.isdecimal(), 'aa15'.isdecimal())
# Output:
# True False
# False True False