-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcampusx_session3.py
218 lines (144 loc) · 4.28 KB
/
campusx_session3.py
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
# ********* nested loop *******
'''for i in range(1,5):
for j in range(1,5):
print(i,j)
rows = int(input('enter number of rows'))
for i in range(1,rows+1):
for j in range(1,i+1):
print('*',end='')
print()
#**************** Loop Control Statement*************#
# Break
for i in range(1,10):
if i == 5:
break
print(i)
# Continue
for i in range(1,10):
if i == 5:
continue
print(i)
# Pass
for i in range(1,10):
pass
#**********String***************
# Strings are sequence of Characters
# In Python specifically, strings are a sequence of Unicode Characters
# Creating Strings
# Accessing Strings
# Adding Chars to Strings ---> not possible
# Editing Strings ---> not possible
# Deleting Strings ---> not possible (you can delete whole string with use of del function)
# Operations on Strings
# String Functions
#Creating Stings
s = 'hello'
s = "hello"
# multiline strings
s = ''hello''
s = """hello"""
s = str('hello')
print(s)
#Accessing Strings
# Positive Indexing
s = 'hello world'
print(s[4])
# Negative Indexing
s = 'hello world'
print(s[-3])
# Slicing
s = 'hello world'
print(s[6:0:-2])
print(s[::-1]) #output---> dlrow olleh
s = 'hello world'
print(s[-1:-6:-1]) #Always remember the position of (-1) is always greater than the position of (-6)
# the position of (-1) is d
# Editing and Deleting in Strings
# s = 'hello world'
# s[0] = 'H'
# Python strings are immutable ---> immutable means not changeable (you cannot edit and delete a word in string)
# But you can Delete whole string
# del s
# print(s)
#*************Operation on strings**********
# Arithmetic Operations
print('delhi' + ' ' + 'mumbai')
print('delhi'*5)
# Relational Operations
print('delhi' != 'delhi')
print('mumbai' > 'pune') # lexiographically
print('Pune' == 'Pune')
# Logical Operations
print('hello' and 'world')
print('hello' or 'world')
print('' and 'world')
print('' or 'world')
print(not 'hello')
# Loops on Strings
for i in 'hello':
print(i)
# Membership operators
print('D' in 'delhi')
#********** Len Function***********
print(len('hello world'))
s = 'rajnish'
print(len(s))
#********** Max Function *******
print(max('hello world'))
#********** Min Function *******
print(min('hello world'))
#************ Sorted Function *********
print(sorted('hello world'))
print(sorted('hello world',reverse=True))
#*********** Capitalize Function**********
s = 'hello world'
print(s.capitalize())
print(s)
#*********** Title Function**********
print(s.title())
#*********** Upper Function**********
print(s.upper())
#*********** Lower Function**********
print(s.lower())
#***********Swapcase Function*********
s1 = 'raJniSH'
print(s1.swapcase())
#***********Count/Find/Index********
print('my name is rajnish'.count('i'))
print('my name is rajnish'.find('x')) # --->they throw -1 if word is not present
# print('my name is rajnish'.index('x')) # --->they throw error if word is not present
# ****************endswith/startswith**********
s1 = 'my name is rajnish'
print(s1.startswith('my'))
print(s1.endswith('sh'))
#************ Format Function ************
name = 'Rajnish'
gender = 'male'
print('Hi my name is {1} and I am a {0}'.format(gender,name))
# ************isalnum/ isalpha/ isdigit/ isidentifier******
print('nitish1234%'.isalnum()) #---> if all digit are alphabet or all number not any identifier here % is identifier
print('nitish'.isalpha())
print('123abc'.isdigit())
print('first-name'.isidentifier())'''
#************** Split and Join Function *********
s1 = "my name is rajnish patel"
print(s1.split())
s2 = ' '
s3 = ['hi', 'my', 'name', 'is', 'nitish']
print(s2.join(s3))
#***************Replace Function*******
print('hi my name is nitish'.replace('nitish','campusx'))
#************strip***********
print('nitish raj '.strip())
# Write a program that can check whether a given string is palindrome or not.
# abba
# malayalam
s = input('enter the string')
flag = True
for i in range(0,len(s)//2):
if s[i] != s[len(s) - i -1]:
flag = False
print('Not a Palindrome')
break
if flag:
print('Palindrome')