Close
Close

Practice questions on Python Strings

Level 1

1.
Write the string after the first occurrence of ',' and the string after the last occurrence of ',' in the string "Hello, Good, Morning". World".

2.
Write a program to print every character of a string entered by user in a new line using loop.
a = raw_input()
for i in a:
  print i

3.
Write a program to find the length of the string "refrigerator" without using len function.
a = "refrigerator"
count = 0
for i in a:
  count = count+1
print count

4.
Write a program to check if the letter 'e' is present in the word 'Umbrella'.
print 'e' in 'Umbrella'

5.
Write a program to check if the word 'orange' is present in the "This is orange juice".
a = "This is orange juice"
print 'orange' in a.split()

6.
Write a program to find the first and the last occurence of the letter 'o' and character ',' in "Hello, World".
Run a loop from front and one from back.

7.
Write a program that takes your full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is. For example, if your name is Robert Brett Roser, then the output should be R.B.Roser.
a = "Robert Brett Roser"
a = a.split()
b = a[0][0]+". "+a[1][0]+". "+a[2]
print b

8.
Check the occurrence of the letter 'e' and the word 'is' in the sentence "This is umbrella".

9.
Write a program to find the number of vowels, consonents, digits and white space characters in a string.

10.
Write a program to make a new string with all the consonents deleted from the string "Hello, have a good day".
a = ['a','e','i','o','u','A','E','I','O','U',' ']
b = "Hello, have a good day"
for i in b:
  if i not in a:
    b = b[:b.index(i)]+b[b.index(i)+1:]
print b

Level 2

1.
Write a program to find out the largest and smallest word in the string "This is an umbrella".
a = "This is an umbrella"
a = a.split()
maxx = a[0]
max_len = len(a[0])
for i in a:
  if len(i)>max_len:
    max_len = len(i)
    maxx = i
print maxx
#similarly find minimum

2.
Write a program to check if a given string is a Palindrome.
A palindrome reads same from front and back e.g.- aba, ccaacc, mom, etc.
a = raw_input()
#a[:] will give the whole string
print a[:]
#a[::-1] -1 is step. It will reverse the string
print a[::-1]
print a == a[::-1]
#There are lot of other ways to solve this problem

3.
Write down the names of 10 of your friends in a list and then sort those in alphabetically ascending order.
Do it yourself

4.
Write a program to make a new string with the word "the" deleted in the sentence "This is the lion in the cage".
a = "This is the lion in the cage"
a = a.split()
for i in a:
  if i == "the":
    #a.index(i) will give the index of i in a
    del(a[a.index(i)])
print a
print " ".join(a)

5.
Write a program to check if the two strings entered by user are anagrams or not. Two words are said to be anagrams if the letters of one word can be rearranged to form the other word. For example, jaxa and ajax are anagrams of each other.
Store every character of the strings in two list and then compare the list

Level 3

Ask Yours
Post Yours