Chapter 7 Strings in Python-Codes
Chapter 7 Strings in Python-Codes
1.Write a program that takes a string as input and counts the occurrence of each vowel in the given string.
str=input(‘Enter a string:’)
c=0
for ch in str:
if ch in ‘aeiouAEIOU’:
c=c+1
print(‘No of vowels=’,c)
2. Write a program that takes a string as input and displays the number of words in the given string.
str=input("Enter a string")
c=0
lwords=str.split()
for word in lwords:
c=c+1
print('No of the word =', c)
3.Write a program that reads a line, then count how many times the word ‘is’ appears in the line and
displays the count.
str=input("Enter a string")
c=0
lwords=str.split()
for word in lwords:
if word==”is”:
c=c+1
print(“No of the word 'is' =”, c)
4.Write a program to remove ‘i’ from the string.
str=input('Enter a string:')
nstr=''
for ch in str:
if ch!='i':
nstr=nstr+ch
print(nstr)
5.Wrte a program to input a multi-word string and convert it to title case, where the first letter of each
word is capitalized.
str=input('Enter a string:')
nword=''
lwords=str.split()
for word in lwords:
nword=nword+word.title()+' '
nword=nword.rstrip()
print(nword)
6. Write a Python program that takes a multi-word string as input, where each word is separated by a
space. Replace all spaces with hyphens ('-') and display the modified string.
str=input(‘Enter a string:’)
nstr=’’
for ch in str:
if ch==’ ‘:
ch=’-‘
nstr=nstr+ch
print(nstr)
7. Write a program that accepts a string input and convert all uppercase letters to lowercase, lowercase
letters to uppercase, while leaving all other characters unchanged.
str=input('Enter a string:')
nstr=''
Chapter-7 Strings in Python
for ch in str:
if ch.isupper():
nstr=nstr+ch.lower()
elif ch.islower():
nstr=nstr+ch.upper()
else:
nstr=nstr+ch
print(nstr)
8.Write a program that takes a string as input and returns a new string with every other letter capitalized.
For example, given the input 'passion', the output should be 'pAsSiOn'.
str=input('Enter a string:')
size=len(str)
nstr=''
for i in range(0,size):
if i%2==0:
nstr=nstr+str[i].upper()
else:
nstr=nstr+str[i]
print(nstr)
9. Write a Python program that takes a string as input from the user and prints the string in reverse order.
str=input('Enter a string:')
size=len(str)
for i in range(size-1,-1,-1):
print(str[i],end='')
10.Write a Python program that takes a string as input from the user and outputs a new string with the characters in reverse order
str=input('Enter a string:')
size=len(str)
nstr=''
for i in range(size-1,-1,-1):
nstr=nstr+str[i]
print(nstr)
11. Write a program that takes a string as input and checks whether the string is a palindrome or not.
str=input('Enter a string:')
size=len(str)
nstr=''
for i in range(size-1,-1,-1):
nstr=nstr+str[i]
if nstr==str:
print(str,"is a palindrome string")
else:
print(str,"is not a palindrome string")
12.Write a program that takes a string as input and displays the longest substring within that string.
str=input('Enter a string:')
size=len(str)
lwords=str.split()
max=0
for word in lwords:
size=len(word)
if size>max:
max=size
maxword=word
print('Longest word is',maxword)