Basic Python – Challenges

Challenges


  Machine Learning in Python

Convert a string to upper case

Take a string from the user as input and convert it to upper case. This has quite a lot of use cases. Forms on the web are a good example of this. When the user enters their name, it is capitalized and stored in the database for consistency

solution

user_input = input ( "Enter your name - ")
print ( user_input.capitalize() )

Check if all the characters in a string are alpha-numeric

Take a string from the user as input and verify if all the characters in the string are alphanumeric. user ids for websites are a good example of this. We don’t want strange characters in the user id, right ? We want them to be either alphabetic or numeric.

solution

user_input = input ( "Choose a user id - ")
if user_input.isalnum() == False :
    print ( "Make sure all the characters are either alphabetic or numeric")

Find the greater of 3 numbers

Without using a data structure (for ex., a list ), find the greatest of 3 numbers. This will let you practice your if-else skills.

solution

print ( "Enter 3 numbers - ")

first   = int( input ( "Enter first number - ") )
second  = int( input ( "Enter second number - ") )
third   = int( input ( "Enter third number - ") )

if first > second :
    greater = first
else :
    greater = second

if third > greater :
    print ("The largest number is - ", third)
else :
    print ( "The largest number is - ", greater)

Find the number of occurrences of a character in a user entered string

You will have to traverse a string in a loop and increment a counter to find the number of occurrences. Do not use the count() function. Check for both capital or small letters.

solution

s = input ( "Enter the string to be searched - ").upper()
a = input ( "Enter the alphabet to be searched - ").upper()

counter = 0

for i in range(len(s)) :
    if a == s[i] :
        counter = counter + 1

print ("The character ",a," occurs ", counter, " number of times")

# Using the count() function - This becomes just a one liner.
# print ("The character ",a," occurs ", s.count(a), " number of times")

Find the number of occurrences of a lower case and upper case characters in a string

In a user entered string, find the number of occurrences of lower case and upper case characters

solution

s = input ( "Enter the string to be searched - ")

counter_l = 0   # counter for lower case
counter_u = 0   # counter for upper case

for i in range(len(s)) :
    if s[i].islower() == True :
        counter_l = counter_l + 1
    elif s[i].isupper() == True:
        counter_u = counter_u + 1

print ( "The number of upper case characters is ", counter_u)
print ( "The number of lower case characters is ", counter_l)

Find the number of occurrences of a lower case and upper case characters in a string

In a user entered string, find the number of occurrences of lower case and upper case characters

solution

s = input ( "Enter the string to be searched - ")

counter_l = 0   # counter for lower case
counter_u = 0   # counter for upper case

for i in range(len(s)) :
    if s[i].islower() == True :
        counter_l = counter_l + 1
    elif s[i].isupper() == True:
        counter_u = counter_u + 1

print ( "The number of upper case characters is ", counter_u)
print ( "The number of lower case characters is ", counter_l)

%d bloggers like this: