PYTHON PRACTICAL SET-A
Q1. PYTHON : (8 marks)
(i) Write a program to check if given string is palindrome or not.
Answer:
# Program to check if a string is a palindrome
def is_palindrome(s: str) -> bool:
s = s.lower().replace(" ", "") # Normalize the string (ignore case and spaces)
return s == s[::-1]
# Test the program
string = input("Enter a string: ")
if is_palindrome(string):
print(f"'{string}' is a palindrome.")
else:
print(f"'{string}' is not a palindrome.")
Answer:
# Program to read and display lines starting with 's'
def display_lines_starting_with_s(filename: str):
file = open(filename, "r")
print("Lines starting with 's':")
for line in file:
if line.strip().lower().startswith('s'):
print(line.strip())
file.close()
# Call the function
display_lines_starting_with_s("poem.txt")
Q.2)
PYTHON PRACTICAL SET-A
Reviewed by Shubham Prajapati
on
January 31, 2025
Rating:
No comments:
If you have any doubt so you can comment me then i will 100% help you ,Through comment Chat