Solution Python Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Solutions to python programs

1.Write a python program that repeatedly asks user for their age and a password until they
provide valid input[age is in digit and password in alphabet and digit only]
#password and age validation

name= input('enter your name ')


while True :
age=input('enter a number for age ')
password=input('enter the password number and alphabets only ')
if age.isdecimal() and password.isalnum() :

print('valid age and password')


break;
else:
print('please reenter age and password')
output

enter your name sony

enter a number for age 20

enter the password number and alphabets only qwe$512g

please reenter age and password

enter a number for age 20

enter the password number and alphabets only qwe23edr

valid age and password

2. Develop a python program to swap cases of a given string

Input : Java

Output : jAVA

#To swap the case of the alphabets in the string

s1=input('enter the string ')

s2=''

for ch in s1:

if ch.isupper():
s2=s2 + ch.lower()

else:

s2=s2 + ch.upper()

print('Swapped case strig is ',s2)

OUTPUT

enter the string COMputer

Swapped case strig is comPUTER

3. Develop a python code to determine whether the given string is a palindrome (OR) not

# to check the string palindrome or not

s1=input('enter the string ')

rev=''

for ch in s1:

rev=ch+rev

print('reversed string is' ,rev)

if s1==rev:

print('it is palindrome')

else:

print('it is not a palindrome')

OUTPUT

i) enter the string hello

reversed string is olleh

it is not a palindrome

ii)enter the string madam

reversed string is madam

it is palindrome

4. Write a program to accept the string and display total number of alphabets

#to count number of alphabets in a string

s1=input('enter the string ')

count=0
for ch in s1:

if ch.isalpha():

count = count + 1

print(' the number of alphbets in a string',count)

output

enter the string qwe23&wer

the number of alphabets in a string 6

5. Develop a program to sort the contents to a text file and write the sorted contents in to a separate
text file

infile = open("example.txt", "r")

myList = infile.readlines()

#Remove trailing \n characters

lineList = []

for line in myList:

lineList.append(line.strip())

#sort the list

lineList.sort()

# Write sorted contents to new file sorted.txt

outfile = open("stext.txt", "w")

for line in lineList:

outfile.write(line + "\n")

infile.close() # Close the input file

outfile.close() # Close the output file

Content of example.txt
Contents of sorted.txt

6. Develop a python program to read and print contents of a text file

infile = open("example.txt", "r")

txt = infile.read()

print(‘contents of the file’)

infile.close()

example.txt contains

python is a programming language

it is object oriented

output

python is a programming language

it is object oriented
7. Develop a program to find the total size of all the files in the given directory

import os

# to find the total size of all files

totalsize=0

for filename in os.listdir('c:\\user'):

totalsize =totalsize + os.path.getsize(os.path.join('c:\\user',filename))

print('total size is ',totalsize)

c:\user has 3 files

marks.doc - 134 bytes

attendance.txt -235 bytes

invite.txt - 70 bytes

output

total size is 439

8. a python program to read and print number of words in a text file

# to count the number of word in a file

f=open('sample.txt', 'r')

text = f.read()

print(text)

words = text.split()

count=0

for w in words:

count =count + 1

print('number of words in a file is ', count)

Contents of sample.txt

Output

number of words in a file is 20


5.

You might also like