Professional Documents
Culture Documents
Experiment No:1 Aim:WAP To Demonstrate Strings in Python:: Name:Pratiksha D. Laldas SE Comps A Roll No:53 Batch A3
Experiment No:1 Aim:WAP To Demonstrate Strings in Python:: Name:Pratiksha D. Laldas SE Comps A Roll No:53 Batch A3
Experiment No:1 Aim:WAP To Demonstrate Strings in Python:: Name:Pratiksha D. Laldas SE Comps A Roll No:53 Batch A3
laldas
SE Comps A
Roll No:53
Batch A3
Experiment No:1
Aim:WAP to demonstrate Strings in Python:
a) A python program to access each element of a string in forward and reverse orders
and check if the entred string is a palindrome.
b) A python program to find the length of a string without using len() function.
c) A python program to know whether a sub string exists in main string or not.
d) A python program to insert a sub string in a particular position.
Theory:
Explain strings in python.
strings in Python are arrays of bytes representing unicode characters.
However, Python does not have a character data type, a single character is simply
a string with a length of 1. Square brackets can be used to access elements of
the string.
Explain and describe different methods to find a sub string in main string.
You can use contains(), indexOf() and lastIndexOf() method to check if
one String contains another String in Java or not. If a String contains another
String then it's known as a substring. The indexOf() method accept a String and
return starting position of the string if it exists, otherwise it will return -1.
a)A python program to access each element of a string in forward and reverse orders and
check if the entred string is a palindrome.
Code:
x=input("Enter a string: ")
X=x.lower()
X=x.strip()
'''l=len(X)
i=l-1
while i>=0:
y[i]=x[i]
print(y[i])
i-=1'''
rstr=X[-1: :-1]
print(rstr,end="")
print()
if X==rstr:
print("Its palindrome")
else:
print("NOt a palindrome")
Output:
Enter a string: PRATIKSHA
AHSKITARP
NOt a palindrome
b)A python program to find the length of a string without using len() function.
Code:
x=input("Enter a string: ")
'''y=print(x[-1])
print(x.index())'''
w=1
c=0
for i in x:
c+=1
if i==" ":
w+=1
print(c)
print(w)
print(len(x))
Output:
Enter a string: Pratiksha Laldas
16
2
16
c)A python program to know whether a sub string exists in main string or not.
Code:
str="I am Here"
sub="am"
n=str.find(sub,0,len(str))
if n==-1:
print("not found")
else:
print(sub,"is found at",n)
Output:
am is found at 2
print(new)
print(' '.join(new))
Output:
['M', 'o', 'n', 'd', 'a', 'y', ' ', 'i', 's', ' ', 'a', ' ', 'h', 'o', 'l', 'i', 'd', 'a', 'y']
Monday is a holiday