0% found this document useful (0 votes)
6 views12 pages

Python String and File Operations

This document outlines an assignment for a B.Tech in Robotics Engineering student at Dr. APJ Abdul Kalam School of Engineering, focusing on problem-solving with Python. It includes various programming tasks related to string manipulation and file operations, along with sample code for each task. The assignment is due on 10/11/2025 and carries a maximum of 10 marks.

Uploaded by

eswarsonu888
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Python String and File Operations

This document outlines an assignment for a B.Tech in Robotics Engineering student at Dr. APJ Abdul Kalam School of Engineering, focusing on problem-solving with Python. It includes various programming tasks related to string manipulation and file operations, along with sample code for each task. The assignment is due on 10/11/2025 and carries a maximum of 10 marks.

Uploaded by

eswarsonu888
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dr.

APJ ABDUL KALAM SCHOOL OF ENGINEERING


DEPARTMENT OF ENGINEERING

NAME OF STUDENT: Eswar N

ROLL NUMBER: 24BTRE111

BRANCH: [Link] IN ROBOTICS ENGINEERING

COURSE NAME & CODE: PROBLEM SOLVIING WITH PYTHON & 10ABTRE24314

ASSIGNMENT NO: 3

DATE OF ISSUED: 26/10/2025

DATE OF SUBMISSION: 10/11/2025

MAX MARKS: 10

Signature of Student Signature of the course instructor


Module 3: Python - Manipula ng strings and File opera ons
Choice Based Assignment – 3
Sec on A: Manipula ng Strings
Q1) String Analyzer: Write a Python program that takes a sentence as input and prints the number of vowels,
consonants, words, and uppercase/lowercase le ers?
Ans1)
Python code:
# String Analyzer Program

# Take input from the user


sentence = input("Enter a sentence: ")

# Ini alize counters


vowels = 0
consonants = 0
uppercase = 0
lowercase = 0

# Define vowels for checking


vowel_le ers = "aeiouAEIOU"
for char in sentence:
if [Link](): # Check if it's a le er
if char in vowel_le ers:
vowels += 1
else:
consonants += 1
if [Link]():
uppercase += 1
elif [Link]():
lowercase += 1
# Count words (split by spaces)
words = len([Link]())

# Display results
print("\n--- String Analysis ---")
print("Total vowels:", vowels)
print("Total consonants:", consonants)
print("Total words:", words)
print("Uppercase le ers:", uppercase)
print("Lowercase le ers:", lowercase)

Output:

Q2) Title Forma er: Write a program that takes a paragraph and capitalizes the first le er of every sentence?
Ans 2)
Python Code:
# Title Forma er Program
text = input("Enter a paragraph: ").strip()

sentences = [Link]('.')

# Capitalize the first le er of each sentence


forma ed = '. '.join([Link]().capitalize() for s in sentences if [Link]())

# Display forma ed paragraph


print("\n--- Forma ed Paragraph ---")
print(forma ed + ".")
Output:

Q3) Character Frequency Counter: Input a string and display how many mes each character occurs using a
dic onary?
Ans 3)
Python Code:
# Character Frequency Counter
s = input("Enter a string: ")

# Create dic onary to store frequency


freq = {}

for ch in s:
freq[ch] = [Link](ch, 0) + 1

print("\n--- Character Frequency ---")


for k, v in [Link]():
print(f"'{k}': {v}")
Output:
Q4) Password Strength Checker: Create a program that checks whether a password is strong (at least 8 chars, 1
uppercase, 1 lowercase, 1 digit, 1 special character)?
Ans 4)
Python Code:
import re
password = input("Enter your password: ")
length = len(password) >= 8
upper = [Link](r"[A-Z]", password)
lower = [Link](r"[a-z]", password)
digit = [Link](r"\d", password)
special = [Link](r"[!@#$%^&*(),.?\":{}|<>]", password)
# Check all condi ons
if all([length, upper, lower, digit, special]):

print("Strong Password ")


else:

print("Weak Password ")


print("Requirements: at least 8 chars, 1 uppercase, 1 lowercase, 1 digit, 1 special char")
Output:

Q5) Reverse Words in a Sentence: Input 'Python is fun' → Output 'fun is Python'.
Ans 5)
Python Code:
# Reverse Words in a Sentence

sentence = input("Enter a sentence: ")

# Split the sentence into words, reverse the list, and join back
reversed_sentence = ' '.join([Link]()[::-1])

print("Reversed Sentence:", reversed_sentence)

Output:

Sec on B: Reading and Wri ng Files


Q6) File Word Counter: Read a text file ([Link]) and print the number of lines, words, and characters?
Ans 6)
Text File info:
Hello, this is a sample text file.
It contains mul ple lines.
Each line has some words and characters.
Python makes text processing easy!
Coun ng lines, words, and characters is simple.

Python Code:
# File Word Counter

file_path = r"C:\Users\Rudrik Joshi\Downloads\[Link]" # Use raw string

try:
# Open the file in read mode
with open(file_path, "r") as file:
lines = fi[Link]()

# Ini alize counters


num_lines = len(lines)
num_words = sum(len([Link]()) for line in lines)
num_chars = sum(len(line) for line in lines)

# Display results
print(f"Lines: {num_lines}")
print(f"Words: {num_words}")
print(f"Characters: {num_chars}")

except FileNotFoundError:
print(f"Error: File not found at {file_path}")
except Excep on as e:
print(f"An error occurred: {e}")

Output:
Q7) Copy File Content: Copy the contents of [Link] into des na [Link]?
Ans 7)
Source file content:

Python Code:
# File Copy Program

source_path = r"C:\Users\Rudrik Joshi\Downloads\[Link]"


des na on_path = r"C:\Users\Rudrik Joshi\Downloads\des na [Link]"

try:
# Open source file in read mode and des na on file in write mode
with open(source_path, "r") as src_file:
content = src_fi[Link]() # Read en re content

with open(des na on_path, "w") as dest_file:


dest_fi[Link](content) # Write content to des na on

print("File copied successfully!")

except FileNotFoundError:
print(f"Error: Source file not found at {source_path}")
except Excep on as e:
print(f"An error occurred: {e}")

Des na on file content:


Output:

Q8) File Extension Checker ([Link]): Ask for a file path and print file name, directory name, and file extension?
Ans 8)
Python Code:
import os

# Ask the user for a file path


file_path = input("Enter the full file path: ")

file_name = [Link](file_path)

dir_name = [Link](file_path)

file_extension = [Link](file_path)[1]
print(f"File Name: {file_name}")
print(f"Directory: {dir_name}")
print(f"File Extension: {file_extension}")
Output:

Q9) Search Word in File: Input a word and search for its occurrences in a file. Display how many mes it
appears?
Ans 9)
Python code:
import os

file_path = input("Enter the file path: ").strip()


search_word = input("Enter the word to search: ").strip()

# Convert to absolute path


file_path = [Link](file_path)

try:
# Open the file in read mode

with open( r"C:\Users\Rudrik Joshi\Downloads\[Link]" , "r", encoding="u -8") as file: # Use u -8 to


avoid encoding issues
content = fi[Link]()

# Count occurrences (case-insensi ve)


count = [Link]().count(search_word.lower())

print(f"The word '{search_word}' appears {count} me(s) in the file.")


except FileNotFoundError:
print(f"Error: File not found at {file_path}")
except IsADirectoryError:
print(f"Error: Path is a directory, not a file.")
except Excep on as e:
print(f"An error occurred: {e}")

Output:

Q10) Append Data to File: Write a program that appends a user-entered line to an exis ng file?
Ans 10)
Before Appending:

Python Code:
# Append Data to File
# Ask for file path
file_path = input("Enter the file path: ").strip()

# Ask for the line to append


line_to_append = input("Enter the line you want to append: ")

try:
# Open the file in append mode
with open(r"C:\Users\Rudrik Joshi\Downloads\[Link]", "a", encoding="u -8") as file:
fi[Link](line_to_append + "\n") # Add newline at the end

print("Line appended successfully!")

except FileNotFoundError:
print(f"Error: File not found at {file_path}")
except IsADirectoryError:
print(f"Error: Path is a directory, not a file.")
except Excep on as e:
print(f"An error occurred: {e}")

A er Appending:

Output:

You might also like