0% found this document useful (0 votes)
2 views3 pages

Lab 3

Uploaded by

drshipray
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)
2 views3 pages

Lab 3

Uploaded by

drshipray
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
You are on page 1/ 3

FACULTY OF TECHNOLOGY

Computer Engineering
01CE1705 – Programming with Python– Lab Manual

PRACTICAL-3

AIM: a. Write a program to check whether the given string is


palindrome or not.

Source Code:
def is_palindrome(input_string):

# Convert the input string to lowercase and remove non-alphanumeric


characters
clean_string = ''.join(char.lower() for char in input_string if
char.isalnum())

# Check if the clean_string reads the same forward and backward


return clean_string == clean_string[::-1]

# Test the function


input_string = input("Enter a string: ")
if is_palindrome(input_string):

print("The given string is a palindrome.")

else:
print("The given string is not a palindrome.")

Output:

1
FACULTY OF TECHNOLOGY
Computer Engineering
01CE1705 – Programming with Python– Lab Manual

AIM: b. Write a program that accepts a string from


user and performs the following operations:
i) Print the string in reverse order
ii) Print all the odd indexed characters of the
string
iii) Print the count of all the vowels in the string
iv) Print the count of the frequency of an input
character in the string

Source Code:

def reverse_string(input_string):
# Printing the string in reverse order
print("Reversed string:", input_string[::-1])

def odd_indexed_characters(input_string):
# Printing all the odd indexed characters of the string
print("Odd indexed characters:", input_string[1::2])

def count_vowels(input_string):
# Counting the number of vowels in the string
vowels = "aeiouAEIOU"
vowel_count = sum(1 for char in input_string if char in vowels)
print("Count of vowels:", vowel_count)

def count_character_frequency(input_string, target_char):


# Counting the frequency of an input character in the string
char_frequency = input_string.count(target_char)
print(f"Frequency of '{target_char}' in the string:", char_frequency)

def main():
input_string = input("Enter a string: ")
reverse_string(input_string)
odd_indexed_characters(input_string)
count_vowels(input_string)

2
FACULTY OF TECHNOLOGY
Computer Engineering
01CE1705 – Programming with Python– Lab Manual

target_char = input("Enter a character to find its frequency in the


string: ")
count_character_frequency(input_string, target_char)

main()

Output:

You might also like