30-Days-Of-Python
247 строк · 7.0 Кб
1
2# Single line comment
3letter = 'P' # A string could be a single character or a bunch of texts
4print(letter) # P
5print(len(letter)) # 1
6greeting = 'Hello, World!' # String could be a single or double quote,"Hello, World!"
7print(greeting) # Hello, World!
8print(len(greeting)) # 13
9sentence = "I hope you are enjoying 30 days of python challenge"
10print(sentence)
11
12# Multiline String
13multiline_string = '''I am a teacher and enjoy teaching.
14I didn't find anything as rewarding as empowering people.
15That is why I created 30 days of python.'''
16print(multiline_string)
17# Another way of doing the same thing
18multiline_string = """I am a teacher and enjoy teaching.
19I didn't find anything as rewarding as empowering people.
20That is why I created 30 days of python."""
21print(multiline_string)
22
23# String Concatenation
24first_name = 'Asabeneh'
25last_name = 'Yetayeh'
26space = ' '
27full_name = first_name + space + last_name
28print(full_name) # Asabeneh Yetayeh
29# Checking length of a string using len() builtin function
30print(len(first_name)) # 8
31print(len(last_name)) # 7
32print(len(first_name) > len(last_name)) # True
33print(len(full_name)) # 15
34
35#### Unpacking characters
36language = 'Python'
37a,b,c,d,e,f = language # unpacking sequence characters into variables
38print(a) # P
39print(b) # y
40print(c) # t
41print(d) # h
42print(e) # o
43print(f) # n
44
45# Accessing characters in strings by index
46language = 'Python'
47first_letter = language[0]
48print(first_letter) # P
49second_letter = language[1]
50print(second_letter) # y
51last_index = len(language) - 1
52last_letter = language[last_index]
53print(last_letter) # n
54
55# If we want to start from right end we can use negative indexing. -1 is the last index
56language = 'Python'
57last_letter = language[-1]
58print(last_letter) # n
59second_last = language[-2]
60print(second_last) # o
61
62# Slicing
63
64language = 'Python'
65first_three = language[0:3] # starts at zero index and up to 3 but not include 3
66last_three = language[3:6]
67print(last_three) # hon
68# Another way
69last_three = language[-3:]
70print(last_three) # hon
71last_three = language[3:]
72print(last_three) # hon
73
74# Skipping character while splitting Python strings
75language = 'Python'
76pto = language[0:6:2] #
77print(pto) # pto
78
79# Escape sequence
80print('I hope every one enjoying the python challenge.\nDo you ?') # line break
81print('Days\tTopics\tExercises')
82print('Day 1\t3\t5')
83print('Day 2\t3\t5')
84print('Day 3\t3\t5')
85print('Day 4\t3\t5')
86print('This is a back slash symbol (\\)') # To write a back slash
87print('In every programming language it starts with \"Hello, World!\"')
88
89## String Methods
90# capitalize(): Converts the first character the string to Capital Letter
91
92challenge = 'thirty days of python'
93print(challenge.capitalize()) # 'Thirty days of python'
94
95# count(): returns occurrences of substring in string, count(substring, start=.., end=..)
96
97challenge = 'thirty days of python'
98print(challenge.count('y')) # 3
99print(challenge.count('y', 7, 14)) # 1
100print(challenge.count('th')) # 2`
101
102# endswith(): Checks if a string ends with a specified ending
103
104challenge = 'thirty days of python'
105print(challenge.endswith('on')) # True
106print(challenge.endswith('tion')) # False
107
108# expandtabs(): Replaces tab character with spaces, default tab size is 8. It takes tab size argument
109
110challenge = 'thirty\tdays\tof\tpython'
111print(challenge.expandtabs()) # 'thirty days of python'
112print(challenge.expandtabs(10)) # 'thirty days of python'
113
114# find(): Returns the index of first occurrence of substring
115
116challenge = 'thirty days of python'
117print(challenge.find('y')) # 5
118print(challenge.find('th')) # 0
119
120# format() formats string into nicer output
121first_name = 'Asabeneh'
122last_name = 'Yetayeh'
123job = 'teacher'
124country = 'Finland'
125sentence = 'I am {} {}. I am a {}. I live in {}.'.format(first_name, last_name, job, country)
126print(sentence) # I am Asabeneh Yetayeh. I am a teacher. I live in Finland.
127
128radius = 10
129pi = 3.14
130area = pi # radius ## 2
131result = 'The area of circle with {} is {}'.format(str(radius), str(area))
132print(result) # The area of circle with 10 is 314.0
133
134# index(): Returns the index of substring
135challenge = 'thirty days of python'
136print(challenge.find('y')) # 5
137print(challenge.find('th')) # 0
138
139# isalnum(): Checks alphanumeric character
140
141challenge = 'ThirtyDaysPython'
142print(challenge.isalnum()) # True
143
144challenge = '30DaysPython'
145print(challenge.isalnum()) # True
146
147challenge = 'thirty days of python'
148print(challenge.isalnum()) # False
149
150challenge = 'thirty days of python 2019'
151print(challenge.isalnum()) # False
152
153# isalpha(): Checks if all characters are alphabets
154
155challenge = 'thirty days of python'
156print(challenge.isalpha()) # True
157num = '123'
158print(num.isalpha()) # False
159
160# isdecimal(): Checks Decimal Characters
161
162challenge = 'thirty days of python'
163print(challenge.find('y')) # 5
164print(challenge.find('th')) # 0
165
166# isdigit(): Checks Digit Characters
167
168challenge = 'Thirty'
169print(challenge.isdigit()) # False
170challenge = '30'
171print(challenge.digit()) # True
172
173# isdecimal():Checks decimal characters
174
175num = '10'
176print(num.isdecimal()) # True
177num = '10.5'
178print(num.isdecimal()) # False
179
180
181# isidentifier():Checks for valid identifier means it check if a string is a valid variable name
182
183challenge = '30DaysOfPython'
184print(challenge.isidentifier()) # False, because it starts with a number
185challenge = 'thirty_days_of_python'
186print(challenge.isidentifier()) # True
187
188
189# islower():Checks if all alphabets in a string are lowercase
190
191challenge = 'thirty days of python'
192print(challenge.islower()) # True
193challenge = 'Thirty days of python'
194print(challenge.islower()) # False
195
196# isupper(): returns if all characters are uppercase characters
197
198challenge = 'thirty days of python'
199print(challenge.isupper()) # False
200challenge = 'THIRTY DAYS OF PYTHON'
201print(challenge.isupper()) # True
202
203
204# isnumeric():Checks numeric characters
205
206num = '10'
207print(num.isnumeric()) # True
208print('ten'.isnumeric()) # False
209
210# join(): Returns a concatenated string
211
212web_tech = ['HTML', 'CSS', 'JavaScript', 'React']
213result = '#, '.join(web_tech)
214print(result) # 'HTML# CSS# JavaScript# React'
215
216# strip(): Removes both leading and trailing characters
217
218challenge = ' thirty days of python '
219print(challenge.strip('y')) # 5
220
221# replace(): Replaces substring inside
222
223challenge = 'thirty days of python'
224print(challenge.replace('python', 'coding')) # 'thirty days of coding'
225
226# split():Splits String from Left
227
228challenge = 'thirty days of python'
229print(challenge.split()) # ['thirty', 'days', 'of', 'python']
230
231# title(): Returns a Title Cased String
232
233challenge = 'thirty days of python'
234print(challenge.title()) # Thirty Days Of Python
235
236# swapcase(): Checks if String Starts with the Specified String
237
238challenge = 'thirty days of python'
239print(challenge.swapcase()) # THIRTY DAYS OF PYTHON
240challenge = 'Thirty Days Of Python'
241print(challenge.swapcase()) # tHIRTY dAYS oF pYTHON
242
243# startswith(): Checks if String Starts with the Specified String
244
245challenge = 'thirty days of python'
246print(challenge.startswith('thirty')) # True
247challenge = '30 days of python'
248print(challenge.startswith('thirty')) # False