#Check if a string is palindrome or not.
#Sample
#Input Output
#noon Palindrome
#Radar Palindrome
#Was it a car or a cat I saw Palindrome
#A man, a plan, a canal: Panama! Palindrome
#Students of MCA Not Palindrome
s=input("ENTER A STRING :")
s1=""
s.lower()
for i in s:
if(i.isalpha()):
s1=s1+i
if (s1[ ::-1].lower ()==s1.lower()):
print("Palindrome String")
else:
print("Not Palindrome String")
ENTER A STRING :A man, a plan, a canal: Panama!
Palindrome String
#Find all duplicate characters in a string.
str1=input("ENTER A STRING")
print("All the duplicate characters in the string are: ")
# Counting every characters of the string
for s in range(0, len(str1)):
count = 1;
for t in range(s+1, len(str1)):
if(str1[s] == str1[t] and str1[s] != ' '):
count = count + 1;
# setting the string t to 0 to avoid printing the characters already
taken
str1 = str1[:t] + '0' + str1[t+1:];
# If the count is greater than 1, the character is considered as
duplicate
if(count > 1 and str1[s] != '0'):
print(str1[s]," - ",count);
ENTER A STRINGmadam
All the duplicate characters in the string are:
m - 2
a - 2
#Find words in a string which are greater than some given length k.
#Sample Input: “If debugging is the process of removing bugs, then
programming
#must be the process of putting them in.”, k=7
#Output: debugging, removing, programming
s=input("ENTER A STRING:")
l=s.split()
l_1=[]
for word in l:
if (len(word)>7):#cheaking is any word lenth is greater than 7
l_1.append(word)
print(" ,".join(l_1))
ENTER A STRING:“If debugging is the process of removing bugs, then
programming must be the process of putting them in
debugging ,removing ,programming
#Accept a string of comma separated words as input and generate a
string of comma separated
#words (from the input string) sorted alphabetically.
#Sample Input: ‘mba, bca, btech, mca’
#Output: ‘bca, btech, mba, mca’
str1=input ("ENTER A STRING ")
list1=str1.split(",")
print(list1)
list1.sort()
sorted_str=",".join(list1)
print (sorted_str)
ENTER A STRING mba, bca, btech, mca
['mba', ' bca', ' btech', ' mca']
bca, btech, mca,mba
#Accept a string of comma separated 4-digit binary numbers as input
and print a comma
#separated string containing the numbers that are divisible by3.
#Sample Input: “0100, 0011, 1010, 1001”
#Output:“0011,1001”
input1=input("Enter comma separated 4 digits binary numbeer :")
l=input1.split(",")
list2=[]
list3=[]
for num in l:
if (int (num,2)%3==0):
list2.append(num)
list3=",".join(list2)
print("The binary numbers list which is divisible by 3:",list3)
Enter comma separated 4 digits binary numbeer :0100, 0011, 1010, 1001
The binary numbers list which is divisible by 3: 0011, 1001
#Move a specified element to the end of a list.
#Sample Input: [‘BSC’, ‘MSC’, BTECH’, ‘MTECH’, ‘BLIB’, MLIB’],
element: ‘MTECH’
#Output: [‘BSC’, ‘MSC’, BTECH’, ‘BLIB’, MLIB’, ‘MTECH’]
string=(input("Enter List Values:"))
finding=input("Enter the lement to move to the end:")
list3=string.split()
if finding in list3:
list3.remove(finding)
list3.append(finding)
print(list3)
else:
print("Element not in list:")
Enter List Values:BSC MSC BTECH MTECH PHD
Enter the lement to move to the end:MSC
['BSC', 'BTECH', 'MTECH', 'PHD', 'MSC']
#Sum the digits of individual elements in a list of numbers.
#Sample Input: [21, 77, 76, 232]
#Output: [3, 14, 13, 7]
user_input=input("Enter the number list:")
pp=list(map(int,user_input.split(",")))#convert every element of
string to integer and store in a list
print(pp)
ff=[]
for num3 in pp:
sum=0
while num3> 0:
sum=sum+num3 % 10
num3//=10
ff.append(sum)
print(ff)
[45, 77, 11, 44]
[9, 14, 2, 8]
#Given a string, generate a list of nonempty prefixes of the string,
ordered from shortest to
#longest.
#Sample Input: “Banana”
#
#Output: ['B', 'Ba', 'Ban', 'Bana', 'Banan','Banana']
input_string=input("Enter the string")
str33=[]
for ch in range(1,len(input_string)+1):
str33.append(input_string[:ch])
print(str33)
Enter the stringpinaki
['p', 'pi', 'pin', 'pina', 'pinak', 'pinaki']
#Given a list of filenames, generate a new list to rename all the
files with extension ‘cpp’ to the
#extension ‘h’.
#Sample Input: ["program.c", "stdio.cpp", "sample.cpp", "a.out",
"math.cpp", "cpp.out"]
#Output: ['program.c', 'stdio.h', 'sample.h', 'a.out', 'math.h',
'cpp.out']\
#Take input from the user
user_input = input("Enter a list of filenames separated by commas: ")
filenames = user_input.split(',')
print (filenames)
# Create a new list for renamed files
renamed_files = []
# Process each filename
for filename in filenames:
if filename.endswith('.cpp'):
renamed_files.append(filename.replace('.cpp', '.h'))
else:
renamed_files.append(filename)
# Output the renamed filenames
print("Renamed filenames:", renamed_files)
['program.c', 'stdio.h', 'sample.h', 'a.out', 'math.cpp', 'cpp.out']
Renamed filenames: ['program.c', 'stdio.h', 'sample.h', 'a.out',
'math.h', 'cpp.out']
#Given a list of courses, create a new list of courses that are
offered by the department of
#Computer Applications (i.e., courses that start with MCA]
#Sample Input: [‘MCA1205’, ‘MCA2125’, ’HUM2191’, ’MTH2102’, ’MCA1295’]
#Output: [‘MCA1205’, ‘MCA2125’, ’MCA1295’]
hh=input ("Enter the courses name with comma separated: ")
courses_list=hh.split(",")
print(courses_list)
new_str=[]
for str123 in courses_list:
if str123.startswith('MCA'):
new_str.append(str123)
print(new_str)
['MCA123', 'FD123', 'MCS34', 'MCA145', 'GH34', 'MCA56', 'MCD34',
'MCA458']
['MCA123', 'MCA145', 'MCA56', 'MCA458']
#A list contains the roll nos. of students of MCA 1st and 2nd year who
enrolled in the debate
#club. The roll no. of the students is prefixed with the year (2
digits) of admission. From that
#given list, create two separate lists for 1st year students and 2nd
year students.
#Sample Input: [2482001, 2482023, 2382022, 2382056, 2482049, 2382036,
2482053]
#Output: [2482001, 2482023, 2482049, 2482053]
#[2382022, 2382056, 2382036]
roll_no=input ("ENTER THE ROLL NOS OF 2ND AND 1ST YEAR STUDENTS WITH
COMMA SEPARATED :")
roll_list=roll_no.split(",")
print(roll_list)
roll_no_1styear=[]
roll_no_2ndyear=[]
for roll in roll_list:
if (roll).startswith('23'):
roll_no_2ndyear.append(roll)
else:
roll_no_1styear.append(roll)
print("Student roll nos of 1st year are:",roll_no_1styear)
print("Student roll nos of 2nd year are:",roll_no_2ndyear)
['2412', '2321', '2444', '2345']
Student roll nos of 1st year are: ['2412', '2444']
Student roll nos of 2nd year are: ['2321', '2345']
#Given a list of names, generate a list where each element is the
surname of the corresponding
#element in the input list.
#Sample Input: [‘A Prasad Sen’, ‘Anita B Kar’, ’Heera Juhuri’, ’Tapasi
Das’, ’Hiya Hembram’]
#output: [‘Sen’, ‘Kar’, ’Juhuri’, ‘Das’, ‘Hembram’]
name_list=input("Enter name list with comma separated:")
list_n =name_list.split(",")
print("The name list :",list_n)
surname=[]
new_surnamelist=[]
for name in list_n:
surname=name.split(" ")
new_surnamelist.append(surname[-1])
print("The surname list:",new_surnamelist)
The name list : ['Pinaki Paul', 'Souvik Gorai', 'Atri Kumar Mondal',
'Ayush Chawdhary', 'Soumen Goswami']
The surname list: ['Paul', 'Gorai', 'Mondal', 'Chawdhary', 'Goswami']
#The str.count() counts the number of non-overlapping occurrences of a
specified substring in a
#tring. E.g., if myStr = ‘Banana’, myStr.count(‘an’) returns 2, but
myStr.count(‘ana’) returns 1.
#Write a Python script that includes the overlapping cases also.
main_str=input("Enter your string:")
print("The mainstring is: ",main_str)
sub_str=input("Enter your substring:")
print("The sub string is : ",sub_str)
lenth11=len(sub_str)
count=0
for i in range(0,len(main_str)+1):
if (main_str[ i:]==sub_str):
count +=1
lenth11 +=1
print(count)
The mainstring is: banana
The sub string is : a
1
#The str.count() counts the number of non-overlapping occurrences of a
specified substring in a
#tring. E.g., if myStr = ‘Banana’, myStr.count(‘an’) returns 2, but
myStr.count(‘ana’) returns 1.
#Write a Python script that includes the overlapping cases also.
# Define the main string and the substring to search for
main_str =input("Enter your strin:")
print("Your given string is :",main_str)
sub_str = input("enter your substring:")
print("Your given sustring is :",sub_str)
count = 0
start = 0
# Loop to find overlapping occurrences
while start < len(main_str):
start = main_str.find(sub_str, start)
if start == -1: # No more occurrences found
break
count += 1
start += 1 # Move to the next character for overlapping counts
# Print the result|
print(f"The substring '{sub_str}' occurs {count} times (including
overlaps) in '{main_str}'.")
Your given string is : banana
Your given sustring is : na
The substring 'na' occurs 2 times (including overlaps) in 'banana'.
#Given a string, generate a string of the longest substring of
consecutive consonants. If more
#than one such substring has the same length, the first should appear
in the string.
given_str=input("Enter your string:")
print("Your given string is:",given_str)
vowel1=set("aeiouAEIOU")
sub_str=""
long_str=""
for ch in given_str:
if ch not in vowel1:
sub_str=sub_str+ch
else:
if len(sub_str)>len(long_str):
long_str=sub_str
sub_str=""
if len(sub_str)>len(long_str):
long_str=sub_str
print(long_str)
Your given string is: python
pyth
pyth