'''
#Different ways to create Strings
a = 'PYTHON' # string with single quotes
b = "IS" # string with double quotes
c = EASY\
TO\
LEARN # string with triple quotes
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=" ")
#Changes in place in not possible in Strings
#That's why it is also known as immutable data type.
name="PYTHON"
print(name)
name[0]='X'
#WAP TO REPLACE SECOND OCCURANCE OF 'H'
#WITH # IN A GIVEN/ENTERED STRING
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
#STRING CONCATENATION OPERATOR +
a = 'PYTHON' # string with single quotes
b = "IS" # string with double quotes
c = EASY\
TO\
LEARN # string with triple quotes
d = EASY
TO
LEARN# string with triple quotes
print (a + " " + b + " " +c + " " + d)
a="HELLO"
b="WORLD" # OUTPUT
print(a+b) # HELLOWORLD
c=10
d=20
print(c+d) #30
print(a+str(c)) #TypeError: c must be str, not int
c='10'
d='20'
print(c+d) #1020
#Ques: WAP to display following pattern (using string)
# $
# $ $
# $ $ $
# $ $ $ $
# $ $ $ $ $
line=''
for i in range(5):
line=line+'$ '
print(line)
#STRING REPLICATION OPERATOR *
a="HELLO"
print(a*3) #HELLOHELLOHELLO
print(3*a) #HELLOHELLOHELLO
print(3*3) # 9
#Ques: WAP to generate following
#==================================================
#==================================================
#Ans:
print('='*50)
print('\n'*5)
print('='*50)
#Ques: WAP to generate following using single print statement
#==================================================
#==================================================
#Ans:
print('='*50,'\n'*5,'='*50)
#MEMBERSHIP OPERARORS in, not in
a="LEARNING PYTHON IS FUN"
print("ING" in a) # True
print("ing" in a) # False
print("ing" not in a) # True
if("ING" in a):
print("T") #True
else:
print("F")
if("ing" not 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
# Determining ASCII/ORDINAL Value of character
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
# Determining character of an ASCII/ORDINAL Value
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[0:10]) #PYTHON PRO
print(name[5:10]) #N PRO
print(name[0:]) #PYTHON PRO
print(name[-7:-1]) #HON PR
print(name[-10:]) #PYTHON PRO
print(name[0:10:2]) #PTO R
str1=input("Enter any string")
str2=''
for i in str1:
if i>='A' and i <='Z':
str2=str2+i
elif i>='a' and i<='z':
str2=str2+chr(ord(i)-32)
else:
str2=str2+i
str1=str2
print(str1)
#STRING FUNCTIONS AND METHODS
str1="python @ 2020"
print(str1.capitalize()) # Python @ 2020
print(str1.upper()) # PYTHON @ 2020
print(str1.find("th",1,len(str1))) # 2
str1="python"
str2="python123"
str3="123"
str4="# @ &"
print(str1.isalnum()) # True (as str1 is containing alphabets)
print(str2.isalnum()) # True (as str2 is containing alphabets or digits)
print(str3.isalnum()) # True (as str3 is containing digits)
print(str4.isalnum()) # False (as str4 is neither containing alphabets nor digits)
str1="python"
str2="python123"
str3="123"
str4="# @ &"
print(str1.isalpha()) # True (as str1 is containing alphabets)
print(str2.isalpha()) # False (as str2 is not containing alphabets only)
print(str3.isalpha()) # False (as str3 is not containing alphabets)
print(str4.isalpha()) # False (as str4 is not containing alphabets)
str1="python"
str2="python123"
str3="123"
str4="# @ &"
print(str1.isdigit()) # False (as str1 is not containing digits)
print(str2.isdigit()) # False (as str2 is not containing digits only)
print(str3.isdigit()) # True (as str3 is containing digits)
print(str4.isdigit()) # False (as str4 is not containing digits)
str1="python"
str2="python123"
str3="123"
str4="# @ &"
str5="Python"
print(str1.islower()) # True ( as all the alphabets are in lower case)
print(str2.islower()) # True ( as alphabets are in lower case)
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)
print(str5.islower()) #False ( as all are alphabets but not in lower case)
str1="python"
str2="PYTHON123"
str3="123"
str4=" # @ &"
str5="Python"
print(str1.isupper()) # False ( as all the alphabets are not in upper case)
print(str2.isupper()) # True ( as all alphabets present are in upper case)
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)
print(str5.isupper()) #False ( as all are alphbets but not in upper case)
str1="python"
str2="PYTHON 123"
str3=" "
print(str1.isspace()) # False (as it does not contain space )
print(str2.isspace()) # False (as it does not contain space only)
print(str3.isspace()) # True( as it contain space only)
print(str1.upper()) # PYTHON
print(str2.lower()) # python 123
str1=" Python " # it is containing 3 leading and 3 trailing spaces
print(len(str1)) # 12
print(str1.lstrip())#Python (without leading spaces)
print(len(str1.lstrip())) #9
print(str1.rstrip())# Python (without Trailing spaces)
print(len(str1.rstrip())) #9
print(str1.strip())#Python (without leading and trailing spaces)
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
#split() it splits the string into list of words.
s1="WAP TO ENTER A STRING"
s2=s1.split()
print(type(s2))
print(s2)
for i in s2:
print(i)
#replace() : it replaces the exiting substring/string with
#the given string and returns the updated string
'''
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 #
s1=input("Enter any string")
s1=s1.replace(" ","#")
print(s1)
#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)
#partition() : this function splits the string at the first
#occurance of seperator, and returns the resultant string
s1="WAP TO ENTER A TO NUMBER"
s1=s1.partition("WAP")
print(s1)
#startswith() : it checks whether the given string starts with
# the given character or set of characters and return True
#otherwise retruns False
s1=input("Enter any string")
if s1.startswith('A'):
print("String startswith A")
else:
print("String does not startswith A")
'''
'''
#WAP TO ENTER A STRING AND A CHARACTER AND CHECK WHETHER
#THE STRING CONTAINS THE CHARACTER OR NOT
str1=input("Enter any string\t")
ch=input("Enter any character\t")
flag=0
for i in str1:
if i==ch:
print("character exists")
flag=1
break
if(flag==0):
print("character does not exists")
# Alternate Method
str1=input("Enter any string\t")
ch=input("Enter any character\t")
if ch in str1:
print("Character exists")
else:
print("Character does not exists")
#WAP TO ENTER A STRING AND A CHARACTER AND CHECK THE OCCURANCE OF A CHARACTER
str1=input("Enter any string\t")
ch=input("Enter any character\t")
c=0
for i in str1:
if i==ch:
c=c+1
print("Total number of occurance of character is \t",c)
# Alternate
str1=input("\n\nEnter any string\t")
ch=input("Enter any character\t")
print("Total number of occurance of character is \t",str1.count(ch))
# 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
str1=input("Enter any string")
for i in range(-1,(-len(str1)-1),-1):
print(str1[i],end="")
# ALTERNATE
str1=input("Enter any string")
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
str1=input("Enter any string\t")
flag=0
for i in range(0,len(str1)//2):
if(str1[i]!=str1[len(str1)-i-1]):
print("not a palindrome")
flag=1
break
if(flag==0):
print("palindrome")
#WAP TO ENTER A STRING AND CHECK WHETHER THE STRING IS PALINDROME OR NOT
#(ALTERNATE)
str1=input("Enter any string")
str2=""
l=len(str1)
for i in range(l-1,-1,-1):
str2=str2+str1[i]
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
str1=input("Enter first string")
str2=input("Enter second string")
str3=''
for i in str1:
if i.isdigit():
str3=str3+i
str4=''
for i in str2:
if i.isdigit():
str4=str4+i
print(str3, " + " ,str4, " = "
print(int(str3)+int(str4))
#***********************************************
#PRACTICAL SESSION 1
#***********************************************
#Q1:WAP to enter a string as a count the total number of words.
#Q2:WAP to enter a string as a count the total number of words
'is' and 'the' seperately.
#Q3:WAP to enter a string as a count the all the combination
of a words entered by user.
for ex: if user enters 'do' the program should be able to
count all the combination of do i.e.dO, Do,DO and do
#Q4: WAP to enter a string and check whether the string is
palindrome or not.
'''