Module 3 Codes
1. Develop python program to generate a triangle using string methods.
side=5
c="*"
for i in range(side):
print((c*i).rjust(side-1)+c+(c*i).ljust(side-1))
Sample output
*
***
*****
*******
*********
2. Develop a python code to determine whether given string is a palindrome or not.
s1="malayalam"
rev=s1[::-1]
print(rev)
print(s1)
if s1==rev:
print("palindrome")
else:
print("not palindrome")
Output:
Palindrome
[Link] a python to swap cases of a given string: Input : Java Output: jAVA
def swap_case_string(str1):
result_str = ""
for i in str1:
if [Link]():
result_str += [Link]()
else:
result_str += [Link]()
return result_str
print(swap_case_string("Java"))
Output: jAVA
4. Develop a program to accept a sentence from the user and display the longest word of
that sentence along with its length.
def longword(string1):
l=0
w=''
words=[Link]()
for word in words:
if(len(word)>l):
l=len(word)
w=word
return l,w
str1='JSSATE is the best engineering college in Bangalore'
l,w=longword(str1)
print('The longest word is:',w)
print('The length of the longest word is:',l)
T
Output:
The longest word is: engineering he longest word is: engineering
The length of the longest word is:11
[Link] a python program that accept a sentence and find the number of words, digits,
uppercase letters and lower case letters.
11e length of the longest word is: 11
s=input(‘Enter the string:’)
w, d, u, l = 0, 0, 0, 0
l_w = [Link]()
w = len(l_w)
for c in s:
if [Link]():
d=d+1
elif [Link]():
u=u+1
elif [Link]():
l=l+1
print ("No of Words: ", w)
print ("No of Digits: ", d)
print ("No of Uppercase letters: ", u)
print ("No of Lowercase letters: ", l)
Sample output:
Enter the string: JSSATE is the number 1 Engineering college in Bangalore
No of Words:9
No of Digits:1
No of Uppercase letters:8
No of Lowercase letters:38
[Link] a python program to read dictionary data and delete any given key entry in
the dictionary.
d = {}
n = int(input("enter number of items"))
for i in range(n) :
k = input("enter a key")
v = input("enter a value")
d[k] = v
print(d)
m = input("enter a key to delete")
del d[m]
print (d)
Output:
Enter number of items: 3
Enter a key orange
Enter a value:14
Enter a key cherry
Enter a value 6
Enter a key banana
Enter a value 12
{'orange': 14, 'cherry':6, ‘banana’:12}
Enter a key to delete cherry
{'orange': 14, ‘banana’:12}
[Link] string slicing operation write python program to reverse each word in a given
string (eg: input: “hello how are you”, output: “olleh woh era uoy”)
def reverseword(s):
word = [Link](" ")
new_word = [i[::-1] for i in word]
new_s = " ".join(nw)
return new_s
s = input(‘Enter the string:’)
print(reverseword(s))
Sample Output:
Enter the string: hello how are you
olleh woh era uoy