Code2pdf 65ced7de9de3b
Code2pdf 65ced7de9de3b
d = EASY
TO
LEARN # string with triple quotes
print(a)
print(b)
print(c)
print(d)
#TRAVERSING IN A STRING
subject="PYTHON"
for ch in subject:
print(ch)
subject="PYTHON"
for ch in subject:
print(ch,end=" ")
name="PYTHON"
print(name)
name[0]='X'
subject ="PHYTHOHN"
str=''
c=0
for ch in subject:
if ch=='H':
c=c+1
if c==2:
str=str+'#'
else:
str=str+ch
subject=str
print(subject)
#STRING OPERATORS
d = EASY
TO
LEARN# string with triple quotes
a="HELLO"
b="WORLD" # OUTPUT
print(a+b) # HELLOWORLD
c=10
d=20
print(c+d) #30
c='10'
d='20'
print(c+d) #1020
# $
# $ $
# $ $ $
# $ $ $ $
# $ $ $ $ $
line=''
for i in range(5):
line=line+'$ '
print(line)
print(a*3) #HELLOHELLOHELLO
print(3*a) #HELLOHELLOHELLO
print(3*3) # 9
#==================================================
#==================================================
#Ans:
print('='*50)
print('\n'*5)
print('='*50)
#==================================================
#==================================================
#Ans:
print('='*50,'\n'*5,'='*50)
print("ING" in a) # True
print("ing" in a) # False
if("ING" in a):
print("T") #True
else:
print("F")
#Ques: WAP to enter username and password and ensure that passward
#should not contain username
#Ans:
user=input("Enter username\t")
pwd=input("Enter password\t")
if user in pwd:
print("Password can not contain username")
else:
print("Welcome to String Manipulation")
#COMPARISON OPERATORS
print("a"=="a") #True
print("a"=="A") #False
print("a"!="A") #True
print("a">"A") #True
print("a"<"A") #False
print("Abc">"abc") #False
a=ord('A')
print(a) # 65
# ordinal values
# 0 to 9 48 to 57
# 'A' to 'Z' 65 to 90
# 'a' to 'z' 97 to 122
a=chr(65)
print(a) #A
#STRING SLICING
# 0 1 2 3 4 5 6 7 8 9
name="PYTHON PRO" # P Y T H O N P R O
#-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
print(name) #PYTHON PRO
print(name[5:10]) #N PRO
print(name[-7:-1]) #HON PR
print(name[0:10:2]) #PTO R
str1="python @ 2020"
print(str1.find("th",1,len(str1))) # 2
str1="python"
str2="python123"
str3="123"
str4="# @ &"
str1="python"
str2="python123"
str3="123"
str4="# @ &"
str1="python"
str2="python123"
str3="123"
str4="# @ &"
str1="python"
str2="python123"
str3="123"
str4="# @ &"
str5="Python"
print(str3.islower()) # False ( as all are not alphabets or all are not in lower case)
print(str4.islower()) # False ( as all are not alphabets or all are not in lower case)
str1="python"
str2="PYTHON123"
str3="123"
str4=" # @ &"
str5="Python"
print(str3.isupper()) # False ( as all are not alphabets or all are not in upper case)
print(str4.isupper()) # False ( as all are not alphabets or all are not in upper case)
str1="python"
str2="PYTHON 123"
str3=" "
print(str1.upper()) # PYTHON
print(len(str1.lstrip())) #9
print(len(str1.rstrip())) #9
print(len(str1.strip())) #6
str1="Python"
print(str1.lstrip("Py")) #thon
print(str1.lstrip("py")) #Python
print(str1.rstrip("thon"))#Py
print(str1.rstrip("on")) #Pyth
'''
s1="WAP TO ENTER A STRING"
print(s1)
s2=''
for i in s1:
if i==' ':
s2=s2+'#'
else:
s2=s2+i
s1=s2
#s1=s1.replace("WAP","WRITE A PROGRAM")
print(s1)
'''
#WAP TO ENTER A STRING AND REPLACE EACH SPACE WITH #
#join() :it joins all the elements of the given string with
#the specified string and returns the resultant string.
s1="@"
s1=s1.join("WAP")
print(s1)
'''
#WAP TO ENTER A STRING AND A CHARACTER AND CHECK WHETHER
#THE STRING CONTAINS THE CHARACTER OR NOT
if(flag==0):
print("character does not exists")
# Alternate Method
#WAP TO ENTER A STRING AND A CHARACTER AND CHECK THE OCCURANCE OF A CHARACTER
# Alternate
# 0 1 2 3 4 5
#WAP TO ENTER A STRING AND DISPLAY THE REVERSE OF THE S T R I N G
print(str1[::-1] # -6-5-4-3-2-1
# ALTERNATE
rev=''
for i in str1:
rev=i+rev
# ALTERNATE
for i in range(len(str1)-1,-1,-1):
print(str1[i],end="")
#WAP TO ENTER A STRING AND CHECK WHETHER THE STRING IS PALINDROME OR NOT
#(ALTERNATE)
if(str1==str2):
print("palindrome")
else:
print("not a palindrome")
#WAP TO ENTER A STRING AND CHECK WHETHER THE STRING IS PALINDROME OR NOT
s1=input("enter any string")
if s1==s1[::-1]:
print("Palnidrome")
else:
print("Not a Palindrome")
#WAP TO ENTER TWO STRINGS AND FIND THE SUM OF THE NUMBER PRESENT IN BOTH
# THE STRING
# FOR EX: IF STR1 = "ABC12D5" AND STR2="XYZ5A75H"
# THEN FIRST STRING CONTAINS 125 AND SECOND STRING CONTAINS 575
#***********************************************
#PRACTICAL SESSION 1
#***********************************************
'''