Class 11 CBSE - Python Strings Programs

Python is designed to understand the user’s responsibility, the protocol, and the ethical issues related to the field of computing.
Python in class 11 for cbse is very handy when it comes to practice on computers but for the beginers there might be some errors and problems such as exception. But it's always advisable to practice program on computer than writing it on paper because it cherishes your practical skills , knowledge and typing speed and you'll come to know new things and if you want to be a software developer someday then its a must call for you.
Program 1: Write a program to count and print words in a string.
Solution:
s="python string example"
s1=""
count_word =0

#iteration to count and store total words
for i in range(0, len(s)):  
  
    if(s[i].isalpha()):
          s1=s1+s[i]
          count_word = count_word + 1;
          
            
# displaying total words
print("Total words in a string="+s1)

#displaying total number of words
print("Total number of words in a string=",count_word)

                        

Program 2: Write a program to print uppercase letters,lowercase letters,digits and special characters in the string entered
Solution:

s="My email id is=xyz123@gmail.com_"
s1=""
s2=""
s3=""
s4=""
s5=""
for i in range(0,len(s)):
      if (s[i].isupper()):
            s1=s1+s[i]
      elif (s[i].islower()):
            s2=s2+s[i]
      elif (s[i].isdigit()):
            s3=s3+s[i]
      else:
            s5=s5+s[i]

#digits in a sentence
print("digits in a sentence are="+s3)

#lowercase letters in a sentence are
print("lowercase letters in a sentence are="+s2)

#uppercase letters in a sentence are
print("uppercase letters in a sentence are="+s1)

#special characters in a sentence are
print("special characters in a sentence are="+s5)

                        

Program 3: Write a program to input two strings into a function and returns the comparison result of the two passed string
Solution:


def string_compare(s1,s2):
      if(len(s1)!=len(s2)):
            return false
      else:
            for i in range(len(s1)):
                  if (s1[i] != s2[i]):
                        return False
                  else:
                        return True

string1=input("Enter the first string:")
string2=input("Enter the second string:")
b=string_compare(string1,string2)
if(b):
      print("Both strings are equal")
else:
      print("Both strings are notequal")
      

                        

Program 4: Write a program to input a string and returns a string having first letter in uppercase
Solution:


def first_capital(sentence):
      new_sentence=""

      #stores each word in a list
      b=sentence.split(" ")
      for i in b:
            new_sentence+=i.capitalize()+" "
      return new_sentence

 # User inputted string
s=input("Enter the string:")

#string with first letter in a word in capital
s1=first_capital(s)
print(s1)

                        

Program 5: Write a program to define a function which takes a string as an argument.And the function should count and display the occurance of words starting with a vowel in the inputted string
Solution:


def vowel_word(s):
      count=0
      word=s.split(" ")
      for i in word:
            if (i[0] in 'aeiou' or i[0] in 'AEIOU'):
                  count+=1
                  print(i)

      print("Totals number of vowel words are:",count)

sentence=input("Enter the string:")
vowel_word(sentence)

                        

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com