Python String & List Programs – Class 11 (Question, Code & Output)
1. Count vowels in a string
Q. Write a Python program to count the number of vowels in a given string.
Code:
s = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for ch in s:
if ch in vowels:
count += 1
print("Number of vowels:", count)
Sample Output:
Enter a string: Welcome
Number of vowels: 3
2. Check if a string is palindrome
Q. Write a Python program to check whether a given string is a palindrome.
Code:
s = input("Enter a string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not a palindrome")
Sample Output:
Enter a string: level
Palindrome
3. Count uppercase, lowercase and digits
Q. Write a Python program to count uppercase letters, lowercase letters and digits in a
string.
Code:
s = input("Enter a string: ")
uc = lc = d = 0
for ch in s:
if [Link]():
uc += 1
elif [Link]():
lc += 1
elif [Link]():
d += 1
print("Uppercase:", uc)
print("Lowercase:", lc)
print("Digits:", d)
Sample Output:
Enter a string: Hello123
Uppercase: 1
Lowercase: 4
Digits: 3
4. Sum of elements of a list
Q. Write a Python program to input numbers in a list and display their sum.
Code:
n = int(input("How many numbers? "))
lst = []
for i in range(n):
[Link](int(input("Enter number: ")))
print("Sum =", sum(lst))
Sample Output:
How many numbers? 4
Enter number: 5
Enter number: 7
Enter number: 3
Enter number: 10
Sum = 25
5. Find largest element in list
Q. Write a Python program to find the largest number in a list.
Code:
lst = eval(input("Enter a list of numbers: "))
print("Largest number:", max(lst))
Sample Output:
Enter a list of numbers: [2, 8, 5, 14, 7]
Largest number: 14
6. Count even and odd numbers in a list
Q. Write a Python program to count the number of even and odd numbers in a list.
Code:
lst = eval(input("Enter list: "))
even = odd = 0
for n in lst:
if n % 2 == 0:
even += 1
else:
odd += 1
print("Even numbers:", even)
print("Odd numbers:", odd)
Sample Output:
Enter list: [1,2,3,4,5,6]
Even numbers: 3
Odd numbers: 3