Swivt Test – 2
1. Print the first and last letter of the string “PythonIsFun”.
str = "PythonIsFun"
print(str[0], str[-1])
Output,
P n
2. Write a program that repeats “I love Python! “ 5 times. (String Replication)
print("I love Python! " * 5)
# str = "I love Python! "
# print(str * 5)
Output,
I love Python! I love Python! I love Python! I love Python! I love Python!
3. Reverse the string “DataScience” using slicing.
print("DataScience reversed:", "DataScience"[::-1])
# str = "DataScience"
# print(str, "reversed: ", str[::-1])
Output,
DataScience reversed: ecneicSataD
4. Combine the strings “Data” and “Science” with a space in between. (String Concatenation)
str1 = "Data"; str2 = "Science"
print(str1 + " " +str2)
Output,
Data Science
5. Create an acronym generator: National Aeronautics Space Administration → NASA.
str = "National Aeronautics Space Administration"
list = str.split(" ")
acronym = ""
for i in list:
acronym += i[0]
print("Acronym: ", acronym)
Output,
Acronym: NASA
6. Make the first letter of each word in “python is awesome” uppercase.
str = "python is awesome"
print(str.title())
Output,
Python Is Awesome
7. Slice out the word “Python” from “Learning Python is fun!”.
print("Learning Python is fun!"[9:16])
# str = "Learning Python is fun!"
# substr = str[9:16]
# print(substr)
Output,
Python
8. Create a string “Python” and print it without the ‘o’.
str = "Python"
for i in str:
if i == 'o':
continue
print (i, end="")
Output,
Pythn
9. Ask the user to input 5 favorite foods one by one and store them in a list. Then print the list in reverse order.
food_list=[]
for i in range(5):
food = input(f"Enter food {i}: ")
food_list.append(food)
print("Regular: ", food_list)
print()
for i in range(len(food_list) -1, -1, -1):
print(f"Food {i+1}: ", food_list[i])
Output,
Regular: ['Momo', 'Kimchi', 'Bread', 'Rice', 'Noodles']
Food 5: Noodles
Food 4: Rice
Food 3: Bread
Food 2: Kimchi
Food 1: Momo
10. Take a list and reverse it.
list=[]
for i in range(5):
food = input(f"Enter food {i}: ")
list.append(food)
print("Regular List: ", list)
list.reverse()
print("Reversed List: ", list)
Output,
Regular List: ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
Reversed List: ['eee', 'ddd', 'ccc', 'bbb', 'aaa']
11. Take a string and print characters at even indexes.
str = input("Input a string:")
print(f"{str}:", str[1::2])
Output,
HIMALAYAS: IAAA
12. Ask the user for a word list and replace all occurrences of “bad” with “good”.
# Ramesh is a bad boy as he has done many bad deeds.
sentence = input("Enter a sentence with a couple of **bad** words: ")
replaced = sentence.replace('bad', 'good')
print(replaced)
Output,
Ramesh is a good boy as he has done many good deeds.
13. Print only the first and last items of this list: [“burger”, “pizza”, “pasta”, “noodles”, “momo”].
list = ["burger", "pizza", "pasta", "noodles", "momo"]
print(list[0], list[-1] )
Output,
burger momo
14. Take an age as input. If the person is 18 or older, print “You can vote!”, else print “Too young to vote.”
age = int(input("Age please: "))
if age >= 18:
print("You can vote!")
else:
print("Too young to vote.")
Output,
You can vote!
15. Ask the user for a number. Print whether it’s even or odd.
number = int(input("Enter a number: "))
if (number%2) == 0:
print(f"{number} is even number.")
else:
print(f"{number} is odd number.")
Output,
79 is odd number.
16. Write a program to check if a number is positive, negative, or zero.
number = int(input("Enter a number: "))
if (number == 0):
print(f"{number} is zero.")
elif (number >0):
print(f"{number} is positive.")
else:
print(f"{number} is negative.")
Output,
-45 is negative.
17. Ask the user for a password. If it matches “secret123”, print “Access granted!”, else “Wrong password!”.
password = input("Enter password:")
if password == "secret123":
print("Access granted!")
else:
print("Wrong password!")
Output,
Wrong password!
18. Name Formatter :Ask the user to enter their full name and print:
- First name
- Last name
- Initials
- Total number of characters (excluding spaces)
name = input("Enter your Name")
list = name.split(" ")
initials = ""
word_count = 0
if len(list) == 2:
print("First Name:", list[0])
print("Last Name:", list[1])
word_count = len(list[0]) + len(list[1])
elif len(list) == 3:
print("First Name:", list[0])
print("Middle Name:", list[1])
print("Last Name:", list[2])
word_count = len(list[0]) + len(list[1]) + len(list[2])
for i in list:
initials += i[0]
print("Initials: ", initials)
print("\nWord count (excluding space): ", word_count)
Output,
First Name: Manoj
Last Name: Lamsal
Initials: ML
Word count (excluding space): 11
19. Reverse Words: Input a sentence and print each word in reverse order: “Hello World” → “olleH dlroW”
str = input("Enter a sentence: ")
print(str, ": ", end="")
list = str.split(" ")
for i in list:
print(i[::-1], end=" ")
Output,
Manoj Kumar Lamsal : jonaM ramuK lasmaL
20. Check Palindrome :Ask for a word and check whether it’s a palindrome.
str = input("Enter a word/number: ")
if str == str[::-1]:
print(f"{str}: is palindrome.")
else:
print(f"{str}: is palindrome.")
Output,
123454321: is palindrome.
21. Multiplication Table Generator :Ask the user for a number and print its multiplication table up to 10.
number = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{number} x {i} =", number*i )
Output,
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
22. Password Checker (3 Chances) :Set a password. Allow the user to guess it with only 3 attempts.
counter = 0
while counter < 3:
password = input("Enter password: ")
if password == "password":
print("Access granted!")
break
else:
counter +=1
if counter == 3:
print("Attempted more than 3 times.")
Output,
Access granted!
23. Print numbers from 1 to 50:
- If divisible by 3, print “Fizz”
- If divisible by 5, print “Buzz”
for i in range(1, 51):
if (i%3) == 0 and (i%5) ==0:
print(f"{i}: Fizz-Buzz")
elif (i%3) == 0:
print(f"{i}: Fizz")
elif (i%5) == 0:
print(f"{i}: Buzz")
Output,
3: Fizz
5: Buzz
6: Fizz
9: Fizz
10: Buzz
12: Fizz
15: Fizz-Buzz
18: Fizz
20: Buzz
21: Fizz
24: Fizz
25: Buzz
27: Fizz
30: Fizz-Buzz
33: Fizz
35: Buzz
36: Fizz
39: Fizz
40: Buzz
42: Fizz
45: Fizz-Buzz
48: Fizz
50: Buzz
24. Prime Number Checker: Ask the user for a number and check if it is prime.
code
Output,
output
25. List Reverser with Loop: Take a list and reverse it.
str = input("Enter a list with seperator (, ):")
list = str.split(", ")
print("Regular: ", list)
reversed_list = []
print()
for i in range(len(list) -1, -1, -1):
reversed_list.append(list[i])
print("Reversed List: ", reversed_list)
Output,
Regular: ['Dinesh', '20', '45.30', 'Manoj', '80', 'Ashwin', 'False']
Reversed List: ['False', 'Ashwin', '80', 'Manoj', '45.30', '20', 'Dinesh']
26. Find and Replace in List Ask the user for a word list and replace all occurrences of “bad” with “good”.
# manoj, 10, bad, sita, 20, nice, niraj, 20, bad, hari, 40, awesome, niraj, 25, bad
words = input("Eneter a list with seperator (, ): ")
list = words.split(", ")
print("original:", list)
for i in range (len(list)):
if list[i] == "bad":
list[i] = "good"
print("replaced: ", list)
Output,
original: ['manoj', '10', 'bad', 'sita', '20', 'nice', 'niraj', '20', 'bad', 'hari', '40', 'awesome', 'niraj', '25', 'bad']
replaced: ['manoj', '10', 'good', 'sita', '20', 'nice', 'niraj', '20', 'good', 'hari', '40', 'awesome', 'niraj', '25', 'good']
27. Pyramid Pattern Input number n, print a pyramid of * with n rows.
*
**
***
****
num = int(input("Number of stars(*): "))
for i in range (1, num+1):
print('*'*i)
Output,
*
**
***
****
*****
******
*******
********
*********
**********
28. Split and Store Words Input a sentence, split it into words, and store them in a list. Print each word on a new line.
str = input("Enter a sentence: ")
list = str.split(" ")
for i in range(len(list)):
print(list[i])
Output,
manoj
is
nice
guy