1.
Count the number of vowels in a
given string
python
string = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in string:
if char in vowels:
count += 1
print("Number of vowels:", count)
2. Check if a string is a palindrome
pytho
string = input("Enter a string: ")
reverse_string = string[::-1]
if string == reverse_string:
print("Palindrome")
else:
print("Not a palindrome")
3. Reverse the words in a string
python
string = input("Enter a sentence: ")
words = string.split()
reversed_words = words[::-1]
result = " ".join(reversed_words)
print("Reversed sentence:", result)
4. Find the frequency of each character
in a string
python
CopyEdit
string = input("Enter a string: ")
frequency = {}
for char in string:
if char in frequency:
frequency[char] += 1
else:
frequency[char] = 1
print("Character frequencies:", frequency)
5. Replace all occurrences of a
substring with another substring
python
CopyEdit
string = input("Enter a string: ")
old_sub = input("Enter substring to replace: ")
new_sub = input("Enter new substring: ")
result = string.replace(old_sub, new_sub)
print("Updated string:", result)
6. Find the longest word in a string
python
CopyEdit
string = input("Enter a sentence: ")
words = string.split()
longest_word = max(words, key=len)
print("Longest word:", longest_word)
7. Remove all punctuation from a string
python
CopyEdit
import string
text = input("Enter a string: ")
cleaned_text = "".join(char for char in text if char not in
string.punctuation)
print("String without punctuation:", cleaned_text)
8. Check if two strings are anagrams
python
CopyEdit
str1 = input("Enter first string: ")
str2 = input("Enter second string: ")
if sorted(str1) == sorted(str2):
print("Anagrams")
else:
print("Not anagrams")
9. Remove all occurrences of a
specified character from a string
python
CopyEdit
string = input("Enter a string: ")
char_to_remove = input("Enter character to remove: ")
result = string.replace(char_to_remove, "")
print("Updated string:", result)
10. Find all palindromic substrings in a
string
python
CopyEdit
string = input("Enter a string: ")
n = len(string)
palindromes = []
for i in range(n):
for j in range(i+1, n+1):
sub = string[i:j]
if sub == sub[::-1] and len(sub) > 1:
palindromes.append(sub)
print("Palindromic substrings:", palindromes)
List Operations
11. Find the second largest element in
a list
python
CopyEdit
numbers = list(map(int, input("Enter numbers
separated by space: ").split()))
numbers = list(set(numbers)) # Remove duplicates
numbers.sort(reverse=True) # Sort in descending
order
if len(numbers) > 1:
print("Second largest number:", numbers[1])
else:
print("No second largest number")
12. Remove all duplicate elements from
a list
python
CopyEdit
numbers = list(map(int, input("Enter numbers
separated by space: ").split()))
unique_numbers = list(set(numbers))
print("List after removing duplicates:",
unique_numbers)
13. Merge two sorted lists into a single
sorted list
python
CopyEdit
list1 = list(map(int, input("Enter first sorted list:
").split()))
list2 = list(map(int, input("Enter second sorted list:
").split()))
merged_list = sorted(list1 + list2)
print("Merged sorted list:", merged_list)
14. Print all even numbers from a given
list
python
CopyEdit
numbers = list(map(int, input("Enter numbers
separated by space: ").split()))
even_numbers = [num for num in numbers if num % 2
== 0]
print("Even numbers:", even_numbers)
15. Rotate a list by a given number of
steps
python
CopyEdit
numbers = list(map(int, input("Enter numbers
separated by space: ").split()))
steps = int(input("Enter number of steps: "))
rotated_list = numbers[steps:] + numbers[:steps]
print("Rotated list:", rotated_list)
16. Find common elements between
two lists
python
CopyEdit
list1 = list(map(int, input("Enter first list: ").split()))
list2 = list(map(int, input("Enter second list: ").split()))
common_elements = list(set(list1) & set(list2))
print("Common elements:", common_elements)
17. Square each element in a list and
return a new list
python
CopyEdit
numbers = list(map(int, input("Enter numbers
separated by space: ").split()))
squared_numbers = [num ** 2 for num in numbers]
print("Squared list:", squared_numbers)
18. Flatten a nested list into a single
list
python
CopyEdit
nested_list = [[1, 2], [3, 4], [5, 6]]
flat_list = [item for sublist in nested_list for item in
sublist]
print("Flattened list:", flat_list)
19. Find the product of all elements in
a list
python
CopyEdit
numbers = list(map(int, input("Enter numbers
separated by space: ").split()))
product = 1
for num in numbers:
product *= num
print("Product of all elements:", product)
These programs do not use def
(functions) and are written in a simple
format for easy understanding. Let me know
if you need any explanations!
2/2
4o