Python Exercise No.
4
1. Write a program to change the given string to a new string with first and last letters
exchanged
# A program to take a string and swap the first and last letters
stg = input('Enter the string : ')
new_stg = stg[-1] + stg[1:-1]+stg[0]
print(new_stg)
2. Write a program to display the string after deleting the nth letter from the user entered
string
# A program to remove the nth letter from the string
stg = input('Enter the string: ')
n = int(input('Enter a number to remove the letter'))
stg1 = stg[0:n-1]
stg2 = stg[n+1:]
final_stg = stg1+stg2
print(f'The string after removing the {n }th letter from the string is {final_stg}')
3. Without using len() function, write a program to find the length of the string entered
# A program to find the length of the string without using len() function
length = 0
stg = input('Enter a string: ')
for char in stg:
length += 1
print(f'Length of the string entered is : { length }')
4. Write a program to count vowels, blanks and consonants in a given sentence
# A program to count vowels, consonants and blanks in a user entered sentence
user_input = input('Enter the sentence: ')
user_input = user_input.lower()
vowels = 0; consonants = 0; blanks = 0
for each_character in user_input:
if (each_character == 'a' or each_character == 'e' or each_character == 'i' or each_character ==
'o' or each_character =='u'):
vowels += 1
elif each_character ==' ':
blanks += 1
else:
consonants += 1
print(f'Number of vowels is: {vowels} and number of blanks is: {blanks} and number of
consonants is:{consonants}')
5. Write a program that accepts a sentence from user and gives the number of words,
digits, uppercase letters and lowercase letters.
# Program to find the number of words, digits, upper case and lower case letters
word_count = 0; digit_count = 0; ucase_count = 0; lcase_count = 0
user_input = input('Enter a sentence containing digits, lower and upper case letters :')
for each_char in user_input:
if each_char.isdigit():
digit_count += 1
elif each_char.isspace():
word_count += 1
elif each_char.isupper():
ucase_count += 1
elif each_char.islower():
lcase_count += 1
else:
pass
print(f' Number of digits is: {digit_count} ')
print(f' Number of words is: {word_count+1} ')
print(f' Number of uppercase letters is is: {ucase_count} ')
print(f' Number of lowercase letters is: {lcase_count} ')
6. Write program to find the given word is a palindrome or not
# A program to find the given word is a palindrome or not
s = input('Enter the word or sentence for palindrome check')
s1 = s.lower()
s2 = s1.split()
l = len(s2)
s3 = ''
for i in range(l):
s3 = s3 + s2[i]
s4 = s3[::-1]
if s3 == s4:
print('You Entered a palindrome')
else:
print('Not a palindrome')
7. Write a program to find the smallest integer whose factorial has 10 zeros in 1st 10 places
# A program to find the smallest number whose factorial has 10 zeros in first 10 places
from math import factorial
for i in range(20,100):
s = str(factorial(i))
length = len(s)
if s[length-10:] == '0000000000':
break
print(i,factorial(i))