Experiment No:1 Aim:WAP To Demonstrate Strings in Python:: Name:Pratiksha D. Laldas SE Comps A Roll No:53 Batch A3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Name:Pratiksha D.

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.

TOOLS USED: Python 3.4.3,Terminal.

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 cases in string.


 s.lower(), s.upper() -- returns the lowercase or uppercase version of the string
 s.strip() -- returns a string with whitespace removed from the start and end
 s.isalpha()/s.isdigit()/s.isspace()... -- tests if all the string chars are in the
various character classes
 s.startswith('other'), s.endswith('other') -- tests if the string starts or ends with
the given other string
 s.find('other') -- searches for the given other string (not a regular expression)
within s, and returns the first index where it begins or -1 if not found
 s.replace('old', 'new') -- returns a string where all occurrences of 'old' have
been replaced by 'new'
 s.split('delim') -- returns a list of substrings separated by the given delimiter.
The delimiter is not a regular expression, it's just text. 'aaa,bbb,ccc'.split(',') -> ['aaa',
'bbb', 'ccc']. As a convenient special case s.split() (with no arguments) splits on all
whitespace chars.
 s.join(list) -- opposite of split(), joins the elements in the given list together
using the string as the delimiter. e.g. '---'.join(['aaa', 'bbb', 'ccc']) -> aaa---bbb---ccc

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

d)A python program to insert a sub string in a particular position.


Code:
str="Monday is a day"
sub="holi"
n=12
new=[]
for i in range(0,n):
new.append(str[i])
for i in sub:
new.append(i)
for i in range(n,len(str)):
new.append(str[i])

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

You might also like