1. 1. Write a program to input a sentence. Find and display the following:
(i) Number of words present in the sentence
(ii) Number of letters present in the sentence
Assume that the sentence has neither include any
digit nor a special character.
Solution:
st=input("Enter a sentance :")
words=st.split()
c=0
print("No of words
:", len(words))
for i in range(len(st)):
if st[i].isalpha():
c=c+1
print("No. of letters
:",c)
2.
Write a program in Python
to accept a word/a String and display the new string after removing all the
vowels present in it.
Sample Input: COMPUTER APPLICATIONS
Sample Output: CMPTR PPLCTNS
st=input("Enter a sentance :")
st1=''
for i in range(len(st)):
if st[i] not in 'AEIOU':
st1=st1+st[i]
elif st[i].isspace():
st1=st+' '
print("No. of letters :",st1)
3.
Write a program in Python to accept a name(Containing three
words) and display only the initials (i.e., first letter of each word).
Sample Input: LAL KRISHNA ADVANI
Sample Output: L K A
st=input("Enter a sentance :").upper()
print("Original String :",st)
words=st.split()
st=''
for word in words:
st=st+word[0]+" "
print("Now string :",st)
4.
Write a program in Python to accept a name containing three
words and display the surname first, followed by the first and middle names.
Sample Input: MOHANDAS KARAMCHAND GANDHI
Sample Output: GANDHI MOHANDAS KARAMCHAND
st=input("Enter a String :")
words=st.split()
L=len(words)
c=st.count(' ')
print(c)
st1=''
surname=words[-1]
for i in range(L-1):
st1=st1+words[i]+" "
print(surname+" "+st1)
5.
Write a program in Python to enter a String/Sentence and
display the longest word and the length of the longest word present in the
String.
Sample Input: “TATA FOOTBALL ACADEMY WILL PLAY
AGAINST MOHAN BAGAN”
Sample Output: The longest word: FOOTBALL: The
length of the word: 8
Solution:
st=input("Enter string :")
words=st.split()
mx=0
for word in words:
if mx<len(word):
st1=word
mx=len(word)
print("The longest word: ",st1)
print("The length of the word: ",mx)
6. Write a program in Python to
accept a word and display the ASCII code of each character of the word.
Sample Input: PYTHON
Sample Output:
ASCII of
P = 80
ASCII of Y = 89
ASCII of T
= 84
ASCII of H
= 72
ASCII of O
= 79
ASCII of N
= 78
Solution::
st=input("Enter
string :")
for i in st:
print("ASCII of ",i," =
", ord(i))
7.
Write a program in Python to accept a String in upper case and
replace all the vowels present in the String with Asterisk (*) sign.
Sample Input: "TATA STEEL IS IN JAMSHEDPUR"
Sample output: T*T* ST**L *S *N J*MSH*DP*R
st=input("Enter string ").upper()
st1=''
for i in st:
if i in 'AEIOU':
st1=st1+'*'
else:
st1=st1+i
print(st1)
8.
Write a program in Python to enter a sentence. Frame a word by
joining all the first characters of each word of the sentence. Display the word.
Sample Input: Vital Information Resource Under
Seize
Sample Output: VIRUS
st=input("Enter
a sentance :").upper()
print("Original
String :",st)
words=st.split()
st=''
for word in
words:
st=st+word[0]+""
print("Now
string :",st)
9.
Write a program in Python to enter a sentence. Display the
words which are only palindrome.
Sample Input: MOM AND DAD ARE NOT AT HOME
Sample Output: MOM
DAD
st=input("Enter
a string :")
words=st.split()
print(words)
for word in
words:
if word==word[::-1]:
print(word)
10. Write a program to accept a
sentence. Display the sentence in reversing order of its word.
Sample Input: Computer is Fun
Sample Output: Fun is Computer
st=input("Enter
a string :")
words=st.split()[::-1]
#print(words)
st1=''
for word in
range(len(words)):
st1=st1+words[word]+' '
print(st1)
11.
Write a program to input a sentence and display the word of
the sentence that contains maximum number of vowels.
Sample Input: HAPPY NEW YEAR
Sample Output: The word with maximum number of
vowels: YEAR
Solution::
st=input("Enter a string ").upper()
print("Original String :",st)
words=st.split()
L=len(words)
mx,st1=0,''
for word in words:
C=0
for letter in word:
if letter in 'AEIOU':
C=C+1
if mx<C:
mx=C
st1=word
print(“The word with maximum number of vowels: ”,st1)
12.
Consider the sentence as given below:
Blue bottle is in Blue bag lying on Blue carpet
Write a program to assign the given sentence to a
string variable. Replace the word Blue with Red at all its occurrence. Display the new string as shown
below:
Red bottle is in Red bag lying on Red carpet
Solution::
st="Blue bottle is in Blue bag lying on Blue carpet"
print(st.replace('Blue','Red'))
#Winout replace() function
st="Blue
bottle is in Blue bag lying on Blue carpet"
words=st.split()
st1=''
for i in words:
if i=='Blue':
st1=st1+'Red'+' '
else:
st1=st1+i+' '
print(st1)
13.
Write a program to accept a word
and convert it into lower case, if it is in upper case. Display the new word by
replacing only the vowels with the letter following it.
Sample Input: computer
Sample Output: cpmpvtfr
Solution::
st=input("Enter
a sentance :")
st1=''
for i in st:
if i in 'AEIOUaeiou':
st1=st1+chr(ord(i)+1)
else:
st1=st1+i
print("No.
of letters :",st1)
14.
Write a program to input a sentence. Create a new sentence by
replacing each consonant with the previous letter. If the previous letter is a
vowel then replace it with the next letter (i.e., if the letter is B then
replace it with C as the previous letter of B is A). Other characters must
remain the same. Display the new sentence.
Sample Input : ICC WORLD CUP
Sample Output : IBB VOQKC BUQ
Solution::
st=input("Enter
a string :").upper()
word=st.split()
st1=''
for i in st:
if i.isspace():
st1=st1+" "
elif i not in 'AEIOU' :
st1=st1+chr(ord(i)-1)
else:
st1=st1+i
st1=st1[:len(st1)-1]+chr(ord(st1[len(st1)-1])+2)
print(st1)
15. Write a python program to reverse words in a given String Python
Sample Input : Narain Ji Srivastava
Output
: Srivastava Ji Narain
sentance= "Narain Ji Srivastava"
s = sentance.split()[::-1] # reversing words in a given string
lst = []
for i in s:
lst.append(i) #
appending reversed words to l
# printing reverse words
print(" ".join(lst))
2. 16. Write a Python
program input String and remove character from string.
str1=input("Input String :")
remove=input("Input letter which you want to remove
letter(s)")
nos=int(input("Press 1-removes all occurrences\n\t2- removes first
occurrences:"))
if nos==1:
newStr=str1.replace(remove,'')
if nos==2:
newStr=str1.replace(remove,'',1)
print(newStr)
3.
17. Write a Python
program to capitalize the first and last character of each word in a string
st =input("Enter a string :")
print("Original string :", st)
a = st.split()
res = []
for i in a:
x =
i[0].upper()+i[1:-1]+i[-1].upper()
res.append(x)
res = " ".join(res)
print("String after:", res)
18. Write a menu driven program to
display the pattern of a String entered by the user. If the user enters choice ‘F’
then it displays the first character of each word. In case the choice is 'L'
then it will display the last character of each word.
Sample Input: HONESTY IS
THE BEST POLICY
|
Enter your
choice: F Sample
Output: H T B P |
Enter your
choice: L Sample
Output: Y S E T Y |
st=input("Enter a string : ")
words=st.split()
ch=input("Enter choice Press \n'F' for display
the first character of each word\n\
'L' for display the last character of each word:
").upper()
if ch=='F':
for word in
words:
print(word[0])
if ch=='L':
for word in
words:
print(word[-1])
No comments:
Post a Comment