5 Questions for TYL on Strings(check if a substring is present in a given string, count vowels)

1.      Write a program to check if the Substring is Present in a Given String

use the in keyword to check if a substring is present in a string or not. That's it!
convert both the string and substring to lower/upper case before checking...
If you're getting the input from the user, make sure you do a strip() especially on the substring...


s = "The big brown wolf"
sub="big"

if sub.lower() in s.lower():
    print(sub," is a substring")
else:
    print(sub," is not a substring")

Output:
big  is  present in  The big brown wolf

bigr  is not a substring


1.      Write a program to count vowels in a given string.

    you're just asked to count vowels... you're not asked to count each vowel and the frequency of it, so dictionary is not required for now. We will learn that program in class a bit later.

   Again, convert the string to a lower case, make use of the "in" keyword.

  Program:

vowels = 'aeiou'
s="programming"
count=0
for ch in s.lower():
    if ch in vowels:
        count+=1




print("The number of vowels : ",count)

Output:
The number of vowels :  3







Comments

Popular posts from this blog

TYL - Food Corner Program

Classes and objects solution

TYL - Salary Hike - Python Problem