0% found this document useful (0 votes)
4 views

Python Prg Ms

Uploaded by

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

Python Prg Ms

Uploaded by

myprojectai9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Python program to count the number of vowels in a string,

For while
text = input("Enter a string: ") text = input("Enter a string: ")
vowels = "aeiouAEIOU" vowels = "aeiouAEIOU"
count = 0 count = 0
i=0
for char in text:
if char in vowels: while i < len(text):
count += 1 if text[i] in vowels:
count += 1
print("Number of vowels:", count) i += 1
****************************
print("Number of vowels:", count)
text = input("Enter a string: ")
count = 0
i=0

while i < len(text):


if text[i].lower() == 'a' or text[i].lower() == 'e' or text[i].lower() == 'i' or text[i].lower() == 'o' or text[i].lower() == 'u':
count += 1
i += 1

print("Number of vowels:", count)

2.Program to count the number of occurrence of a character in a string (using for)


text = input("Enter a string: ")
char_to_count = input("Enter the character to count: ")
count = 0

for char in text:


if char == char_to_count:
count += 1

print(f"Character '{char_to_count}' appears {count} times.")


Using while
text = input("Enter a string: ")
char_to_count = input("Enter the character to count: ")
count = 0
i=0

while i < len(text):


if text[i] == char_to_count:
count += 1
i += 1

print(f"Character '{char_to_count}' appears {count} times.")

3.Program to check whether a string is palindrome or not (for)


text = input("Enter a string: ")

is_palindrome = True

for i in range(len(text) // 2):


if text[i] != text[len(text) - 1 - i]:
is_palindrome = False
break
if is_palindrome:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

Using list
text = input("Enter a string: ")

if text == text[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

Convert to list and reverse


text = input("Enter a string: ")
text_list = list(text)
reversed_list = list(reversed(text_list))

if text_list == reversed_list:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

You might also like