-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_Day_Of_Python_Strings.py
More file actions
211 lines (170 loc) · 5.29 KB
/
Copy path04_Day_Of_Python_Strings.py
File metadata and controls
211 lines (170 loc) · 5.29 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
# name = "karan"
# age = 28
# height = 5.9
# is_developer = True
# print("Name", name)
# print("Age", age)
# print("Height", height)
# print("Developer", is_developer)
# # A list of favourite foods
# favourite_food = ["Pasta", "Pizza", "Momos", "Icecream"]
# print("My favourite foods are")
# for food in favourite_food:
# print("-", food)
age = 80
is_student = True
if age < 13:
print("You are a child")
elif age > 18 and is_student == True:
print("You are a adult teenager")
elif age < 20:
print("You are a teenager")
elif age > 60:
print("You are a senior citizen")
else:
print("You are an adult")
def greet_user(name):
print("Hello", name)
print("Welcome to python")
#input_name = input("Enter your name:")
#greet_user(input_name)
def calculate_square(number):
print("Square of a number is:", number * number)
calculate_square(5)
#Dictionary to story person details
person = {
"name": "Karan",
"age": 25,
"city": "Delhi"
}
print("Name:", person["name"])
print("Age:", person["age"])
print("Name:", person["city"])
for key, value in person.items():
print(key + ":" , value)
letter = 'P'
print(letter)
print(len(letter))
greeting = "Hello world"
print(greeting)
print(len(greeting))
multiline_string = '''I am a teacher and enjoy teaching.
I didn't find anything as rewarding as empowering people.
That is why I created 30 days of python.'''
print(multiline_string)
print(len(multiline_string))
another_multiline_string = """I am a teacher and enjoy teaching.
I didn't find anything as rewarding as empowering people.
That is why I created 30 days of python."""
first_name = "karan"
last_name = "chadha"
space = ' '
full_name = first_name + space + last_name
print(full_name)
print(len(first_name) > len(last_name))
language = "Python"
first_letter = language[0]
print(first_letter)
last_letter = language[-1]
print(last_letter)
#slicing
first_three= language[0:3]
print(first_three)
last_three = language[3:6]
print(last_three)
last_three = language[-3:]
print(last_three)
#skipping character while splitting python strings
language = 'Python'
pto = language[0:6:2]
print(pto)
#string methods
# capitalize(): Converts the first character the string to Capital Letter
challenge = 'thirty days of python'
capital = print(challenge.capitalize())
#count(): returns occurances of substring in string count(substring, start='', end='')
challenge = 'thirty days of python'
print(challenge.count('y'))
print(challenge.count('y', 7, 14))
print(challenge.count('th'))
#endswith() : checks if the string ends with the specified ending
challenge = 'thirty days of python'
print(challenge.endswith('on'))
print(challenge.endswith('py'))
# expandtabs(): Replaces tab character with spaces, default tab size is 8. It takes tab size argument
challenge = 'thirty\tdays\tof\tpython'
print(challenge.expandtabs())
print(challenge.expandtabs(10))
#find() returns the index of the first occurance of a substring
challenge = 'thirty days of python'
print(challenge.find('y'))
print(challenge.find('th'))
#format() format string to a nicer output
first_name = 'Karan'
last_name = 'Chadha'
job = 'Teacher'
country = 'India'
sentence = 'I am {}{}. I live in {}. I am a {}'.format(first_name, last_name, country, job)
print(sentence)
#isalnum(): checks alphanumeric character
challenge = 'ThirtyDaysPython'
print(challenge.isalnum())
challenge = '30DaysPython'
print(challenge.isalnum())
challenge = 'Thirty Days Python'
print(challenge.isalnum())
#isalpha() checks if all the characters are alphabets
challenge = 'ThirtyDaysPython'
print(challenge.isalpha())
num = '123'
print(num.isalpha())
#isDigit checks Digit character
num = '10'
print(num.isdigit())
num = 'thirty'
print(num.isdigit())
# isdecimal():Checks decimal characters
num = '10'
print(num.isdecimal()) # True
num = '10.5'
print(num.isdecimal()) # False
#islower(): checks if all the alphabets in a string are lowercase
challenge = 'thirtydaysofcoding'
print(challenge.islower())
challenge = 'Thirtydaysofcoding'
print(challenge.islower())
#islower(): checks if all the alphabets in a string are uppercase
challenge = 'THIRTYDAYSOFCODING'
print(challenge.isupper())
challenge = 'Thirtydaysofcoding'
print(challenge.isupper())
#isnumeric() checks numeric characters
num = '10'
print(num.isnumeric())
print('Ten'.isnumeric())
#join(): Returns a concatenated string
web_tech = ['HTML', 'CSS', 'Javascript', 'React']
result = '#,'.join(web_tech)
print(result)
#strip(): Removes both leading and trailing characters
challenge = 'thirty days of python'
print(challenge.strip('y')) # 5
#replace(): replace substring inside
challenge = 'thirty days of code'
print(challenge.replace('code','python'))
#split(): split strings from left
challenge = 'thirty days of python'
print(challenge.split())
# title(): Returns a Title Cased String
challenge = 'thirty days of python'
print(challenge.title()) # Thirty Days Of Python
# swapcase(): Checks if String Starts with the Specified String
challenge = 'thirty days of python'
print(challenge.swapcase()) # THIRTY DAYS OF PYTHON
challenge = 'Thirty Days Of Python'
print(challenge.swapcase()) # tHIRTY dAYS oF pYTHON
# startswith(): Checks if String Starts with the Specified String
challenge = 'thirty days of python'
print(challenge.startswith('thirty')) # True
challenge = '30 days of python'
print(challenge.startswith('thirty')) # False