Practical Assignment-4
Practical Assignment-4
Practical Assignment-4
Assignment-4 : Strings
~By- Vishesh Aggarwal
I. Program-1
#1. Write a program to input lines of text from user until ENTER is pressed. Count total c
haracters(incl. spaces), total alphabets,total digits, total special characters/symbols and to
tal words.(Assuming words are separated by one space.)
lines=int(input("Enter no. of lines needed : "))
string=''
count_alph=0
for i in range(len(string)):
if (string[i] >= 'A' and string[i] <= 'Z') or (string[i] >= 'a' and string[i] <= 'z') :
count_alph+= 1
print("Total Number of alphabents :",count_alph)
count_number=0
for i in range(len(string)):
if string[i] >= '0' and string[i] <= '9':
count_number+= 1
print("Total Number of numbers :",count_number)
count_spl=len(string)-count_alph-count_number-string.count(' ')-lines
print("Total Number of Special Characters :",count_spl)
words=string.split()
print("Total Number of words :",len(words))
Output:
Enter no. of lines needed : 4
Enter your data :
Hi!
How are you?
I'll be good in some 1000 days.
Oh, Great!
II. Program-2
#2. Convert a string in Title Case
String=input("Enter the string : ")
print("Your String in title case : \n",String.title())
Output:
Enter the string : Hi, hello, go to hell.
Your String in title case :
Hi, Hello, Go To Hell.
III. Program-3
#3.Creating a new string after deleting all occurences of a character from given string.
String=input('Enter your string : ')
char=input("Enter the character to delete its occurences : ")
print("Your New String: \n",String.replace(char,""))
Output:
Enter your string : Mary Made Magnificent Muffins On Monday.
Enter the character to delete its occurences : M
Your New String:
ary ade agnificent uffins On onday.
IV. Program-4
#4. String should have some digits. Print their sum.
string=input('Enter your string containg some digits : ')
sum=0
for i in range(len(string)):
if string[i] >= '0' and string[i] <= '9':
sum+=int(string[i])
print("The Sum of Digits is",sum)
Output:
Enter your string containg some digits : Hi1! Bye 54321
The Sum of Digits is 16
V. Program-5
#5. Replace space with hyphen.
string=input('Enter your string : ')
print("Your New String : \n",string.replace(" ","-"))
Output:
Enter your string : Hi! Bye! So sorry to leave so soon.
Your New String :
Hi!-Bye!-So-sorry-to-leave-so-soon.
VI. Program-6
#6. Check a string is a palindrome.
string=input('Enter your string : ')
string=string.lower()
rev_str=""
for i in range(1,len(string)+1):
rev_str+=string[-i]
if string==rev_str:
print("Entered string is a palindrome.")
else:
print("Entered string is not a palindrome.")
Output:
Enter your string : Wow
Entered string is a palindrome.