logo codesdope

Practice questions on String

Level 1


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


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


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

print 'e' in 'Umbrella'


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()


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.


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


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


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


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


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


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


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


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

Do it yourself


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
print a
print " ".join(a)


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


Welcome.please sign up.


Welcome.please login.