C.S PROGRAMS
C.S PROGRAMS
PUBLIC SCHOOL
NAME: G.S.NARENDIRAN
SUBJECT: COMPUTER SCIENCE
CLASS: 11
PYTHON PRACTICAL PROGRAMS
EXP.NO: 1
DATE:
AIM: To count the number of times a character occurs in the
given string.
CODE:
inp=input ("enter a string:")
ch=input ("enter a character that is to be counted:")
c= inp.count(ch)
print("the character",ch,"occurs",c,"times")
OUTPUT:
EXP.NO: 2
DATE:
AIM: To replace all vowels in the string with '*'.
CODE:
str = input("Enter the string: ")
newStr = ""
for ch in str :
lch = ch.lower()
if lch == 'a' \
or lch == 'e' \
or lch == 'i' \
or lch == 'o' \
or lch == 'u' :
newStr += '*'
else :
newStr += ch
print(newStr)
OUTPUT:
EXP.NO: 3
DATE:
AIM: To reverse a string and store the reversed string in a new
string.
CODE:
str = input("Enter the string: ")
newStr = ""
for ch in str :
newStr = ch + newStr
print(newStr)
OUTPUT:
EXP.NO: 4
DATE:
AIM: To prompt for a phone number of 10 digits and two dashes,
with dashes
after the area code and the next three numbers. For example, 017-
555-1212 is a legal input.
Display if the phone number entered is valid format or not and
display if the phone number is
valid or not (i.e., contains just the digits and dash at specific places.)
CODE:
phNo = input("Enter the phone number: ")
length = len(phNo)
if length == 12 \
and phNo[3] == "-" \
and phNo[7] == "-" \
and phNo[:3].isdigit() \
and phNo[4:7].isdigit() \
and phNo[8:].isdigit() :
print("Valid Phone Number")
else :
print("Invalid Phone Number")
OUTPUT:
EXP.NO: 5
DATE:
AIM: • prompt the user for a string
• extract all the digits from the string
• If there are digits:
o sum the collected digits together
o print out the original string, the digits, the sum of the digits
• If there are no digits:
o print the original string and a message "has no digits"
CODE: str = input("Enter the string: ")
sum = 0
digitStr = ''
for ch in str :
if ch.isdigit() :
digitStr += ch
sum += int(ch)
if not digitStr :
print(str, "has no digits")
else :
print(str, "has the digits", digitStr, "which sum to", sum)
OUTPUT:
EXP.NO: 6
DATE:
AIM: To prompt the user to type some sentence(s) followed
by "enter". It
should then print the original sentence(s) and the following
statistics relating to the
sentence(s) :
• Number of words
• Number of characters (including white-space and
punctuation)
• Percentage of characters that are alphanumeric
CODE:
. inp=input("enter a string :")
space=0
alnum=0
length=len(inp)
for i in inp:
if i.isspace():
space+=1
elif i.isalnum():
alnum+=1
print("the original sentence is :",inp)
print("the number of words is :",space+1)
print("the length of the sentence is :",length)
print("the percentage of alphanumeric is:",alnum/length*100)
OUTPUT:
EXP.NO: 7
DATE:
AIM: • Repeatedly prompt for a sentence (string) or for 'q' to quit.
• Upon input of a sentence s, print the string produced from s by
converting each lower
case letter to upper case and each upper case letter to lower case.
• All other characters are left unchanged.
CODE:
while True:
str1=input("enter a sentence or enter 'q' to quit:")
newstr=""
if str1=='q':
break
for ch in str1:
if ch.islower():
newstr += ch.upper()
elif ch.isupper():
newstr += ch.lower()
else:
newstr +=ch
print(newstr)
OUTPUT:
EXP.NO: 8
DATE:
AIM: • To take two inputs : the first, an integer and the second, a
string
• from the input string extract all the digits, in the order they
occurred, from the string.
o if no digits occur, set the extracted digits to 0
• add the integer input and the digits extracted from the string
together as integers
• print a string of the form :
"integer_input + string_digits = sum"
CODE:
num = int(input("Enter an integer: "))
str = input("Enter the string: ")
digitsStr = ''
digitsNum = 0;
for ch in str :
if ch.isdigit() :
digitsStr += ch
if digitsStr :
digitsNum = int(digitsStr)
print(num, "+", digitsNum, "=", (num + digitsNum))
OUTPUT:
EXP.NO: 9
DATE:
AIM: to count the number of times the character occurs in a string
a=input("ENTER A STRING")
c=a.count(b)
print(c)
EXP.NO:10
DATE:
AIM: TO PRINT NUMBER OF WORDS
2.:
STRING=input("ENTER A STRING (ONLY WIT SINGLE SPACE BETWEEN THE
ORDS)")
count=0
for i in string:
if ch.isspace():
count+=1
print("NUMBER OF WORDS=",count+1)
EXP.NO:11
DATE:
AIM: CHECKING EQUAL NUMBER OF PARANTHESES
3.
count=0
for i in a:
if i== "(":
count+=1
elif i==")":
count-=1
if count==0:
else:
4.:
I=input('enter a string:')
n=I[::-1]
w=string.split()
longword=""
for i in w:
a=len(i)
if a>len(longword):
longword=i