14 Python Programming
14 Python Programming
Session 14
Q1Prompt user to enter a string and a character. Find
the number of occurrences of that character in the
string.
a=input("enter a string")
b=input("enter a character")
c=a.count(b)
print("total no of occurrence",c)
Q2 Write a function to print the count of all
substring characters in the input string
a=input("enter a string")
b=input("enter a substring")c=a.count(b)
print("total no of occurrence of the substring ",c)
Q3 Write a function to count the total number of
occurrences of all vowels in the string input by the user
a=input("enter a string") output
enter a stringjjiha je nkki uuc88dae
vowel="aeiou" total no of occurrence of the vowel a is 2
a=a.lower() total no of occurrence of the vowel e is 2
total no of occurrence of the vowel i is 2
total=0 total no of occurrence of the vowel o is 0
for char in vowel: total no of occurrence of the vowel u is 2
total number of occurrences of vowels is 8
c=a.count(char)
print("total no of occurrence of the vowel ",char,"is ",c)
total= total +c
print("total number of occurrences of vowels is ",total)
Reverse a string.
Input =“Hello welcome”
Output = “emoclew olleH”
Str=input("enter string")
Revstr=‘’
A=len(Str)
for i in range(0,A):
Revstr= Str[i] +Revstr
print("reverse string is ", Revstr)
Alternate way of presenting
Str=input("enter string") Str=input("enter string") Str=input("enter string")
Revstr=Str[ : : -1] a=len(Str)
Revstr=‘’ Revstr=‘’
Print(Revstr)
for i in Str: for i in range(-1,-a-1,-1):
Revstr= Revstr+Str[i]
Revstr=i+ Revstr print(Revstr)
print(Revstr)
Write a function that takes a sentence as input and
displays number of words in the sentence
#assuming words are separated by def words(Str):
single space prev=" "
count=0
Str= input("enter a sentence") for i in range(0, len(Str)):
A= Str.split() if prev==" " and Str[i]!=" ":
count =count+1
print(len(A))
prev=Str[i]
Eg=“Hello how are you” return count
using Str.split()
S= input("enter a sentence ")
A=[“Hello”,”how”,”are”,”you”] w=words(S)
Len(A)=4 print(w)
S=“AB DS F g”
Previous=“ “ Len(S)= 10
A B D S F g
A B D S F g
Previous current
Use of slicing
S=“HELLO ARUN” Positive Index
0 1 2 3 4 5 6 7 8 9
H E L L O A R U N
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
S[i] refers to character at ith index in the string S[3] refers to “L”
Negative Index
If a character exists at ith position and we want to see if it appeared earlier
We can check earlier characters by checking substring by slicing characters from
location to 0 to i-1
If I =3 S[i]=“L”
Then S[0:i] or S[ :i] refers to substring “HEL”
We can check if “L” is in “HEL” by using syntax
“L” in S[ :i]
Counting number of matching characters in a pair of strings
◦ Step 1 Convert both strings in lowercase/uppercase
◦ Step 2 for every character in string1 do step 3
◦ step 3 if character exists only once in string 1( no duplicates of character exist)
◦ then only do step 4
◦ step 4 count the number of times that character exists in the second string
Eg string 1:abcdgfabcgh
string 2:bagfb will give answer 4
◦ We will check the existence of every character of string1 in string 2
◦ We must ensure that i th character is not already counted as common character
◦ So we check in a slice of characters before i position -> string[ : i]
◦ We extract every character from string 2
◦ Compare characters from both strings
◦ If they match increment count and now there is no need to check for string 1 character in string 2 as it is
already counted
◦ So we can exist the loop
s1=input("string 1 :")
s2=input("string 2 :")
s1=s1.lower()
s2=s2.lower()
count=0
for i in range(0,len(s1)):
char=s1[i]
if not(char in s1[:i]):
for ch in s2:
if char== ch:
count +=1
break
print(count)