1. Home
  2. Docs
  3. Learn Python By Examples
  4. Learn Python by examples – Ex-21-25

Learn Python by examples – Ex-21-25

Learn Python by examples – Ex-21-25

 
Note there are multiple ways to solve a problem.
Here is one solution.
 

Exercise 21: Write a program print “Python is the best programming language” in

1. Uppercase => PYTHON IS THE BEST PROGRAMMING LANGUAGE
2. Lowercase => python is the best programming language
3. Title-case => Python Is The Best Programming Language

 

Solution:
 

Concept:

Using string methods.
 

#upper, lower, title
str1 = "Python is the best programming language"

print(str1.upper())

print(str1.lower())

print(str1.title())

Output:

PYTHON IS THE BEST PROGRAMMING LANGUAGE
python is the best programming language
Python Is The Best Programming Language

 
 

Exercise 22: Write a program to get the below, from a given string ” Python is the best programming language ”

1. Create a word list from the given string
2. Get each word form string
3. Get Python, language, best, programming

 
Concept:

using indexing for list,strip(), split() string methods.

 
Solution:
 

# indexing, strip,split

given_str = " Python is the best programming language     "

#1 word list
given_str = given_str.strip()
word_list = given_str.split()

#world_list = given_str.strip().split()
print(word_list)
print("\n")

#2 each word
for word in word_list:
    print(word)
    
print("\n")

#by indexing
print(word_list[0],word_list[1],word_list[2],word_list[3],word_list[4],word_list[5])

#3 specific indexing, Python, language, best, programming
print(word_list[0], word_list[-1],word_list[-3],word_list[-2])

Output:
['Python', 'is', 'the', 'best', 'programming', 'language']


Python
is
the
best
programming
language


Python is the best programming language
Python language best programming


 
 
Exercise 23: Write a program get ‘50,60’ from string “10,20,30,40,50,60,70”
 
Solution:

 
Concept:
user of split(), join()
get sub string by slicing str[start:end]

 

# split, slice, join

str1 = "10,20,30,40,50,60,70" 

print(str1.split(","))

sub_str = str1.split(',')[4:6] # ['50', '60']
print(sub_str)

new_str =','.join(sub_str)
print(new_str)

    
new_str = ",".join(str1.split(',')[4:6])
 
print(new_str)
 
#using negative index

#new_str = ",".join(str1.split(',')[-3:-1])
 
#print(new_str)

Output:

['10', '20', '30', '40', '50', '60', '70']
['50', '60']
50,60
50,60

 
 

Exercise 24: Write a programming to get sum of digits from string “10,20,30,40,50,60,70”
 
Solution:

 
Concept:

using spilt() string method with comma delimiter,
converting string digits to int
 


#split, int, 
str1 = "10,20,30,40,50,60,70".strip()

num_list = str1.split(',')

total = 0
for num in num_list:
    total = total + int(num)

print(total)


#using map and sum built in

total = sum(map(int,str1.split(',')))

print("2nd Total :", total)



Output:

280
2nd Total : 280

 
 

Exercise 25: Write a program print prime numbers up to 100

 
Solution:

 

Concept:
string indexing and slicing to get required sub part string.

 

#index of and slicing

# str1 = "Python"
# print(str1[1:])
# print(str1[1:-1])
# print(str1[:-2])

# print("\n")

credit_str = 'xxxx-xxxx-8888-xxxx'
 
new_credit_str = credit_str[credit_str.index('8'):credit_str.index('8') + len('8888')]
 
print(new_credit_str)
    
Output:

8888