Python String Programs, Exercises, Examples

Python string is a sequence of characters, such as “hello”. They can be used to represent text in a programming language. Strings can be created by enclosing a sequence of characters between double quotes, such as “hello”, or by using the str() function.

1). Write a Python program to get a string made of the first and the last 2 chars from a given string. If the string length is less than 2, return instead of the empty string

2). Python string program that takes a list of strings and returns the length of the longest string.

3). Python string program to get a string made of 4 copies of the last two characters of a specified string (length must be at least 2).

4). Python string program to reverse a string if it’s length is a multiple of 4.

5). Python string program to count occurrences of a substring in a string.

6). Python string program to test whether a passed letter is a vowel or consonant.

7). Find the longest and smallest word in the input string.

8). Print most simultaneously repeated characters in the input string.

9). Write a Python program to calculate the length of a string with loop logic.

10). Write a Python program to replace the second occurrence of any char with the special character $.
Input = “Programming”
Output = “Prog$am$in$”

11). Write a python program to get to swap the last character of a given string.
Input = “SqaTool”
Output = “IqaTooS”

12). Write a python program to exchange the first and last character of each word from the given string.
Input = “Its Online Learning”
Output = “stI enlino gearninL”

13). Write a python to count vowels from each word in the given string show as dictionary output.
Input = “We are Learning Python Codding”
output = {“We” : 1, “are” : 2, “Learning” : 3, “Python”:1, “Codding”}

14). Write a python to repeat vowels 3 times and consonants 2 times.
Input = “Sqa Tools Learning”
Ouput = “SSqqaaa TToooooollss LLeeeaaarrnniiinngg”

15). Write a python program to re-arrange the string.
Input = “Cricket Plays Virat”
Output = “Virat Plays Cricket”

16). Write a python program to get all the digits from the given string.
Input = “””
Sinak’s 1112 aim is to 1773 create a new generation of people who
understand 444 that an organization’s 5324 success or failure is
based on 555 leadership excellence and not managerial
acumen
“””
Output = [1112, 5324, 1773, 5324, 555]

17). Write a python program to replace the words “Java” with “Python” in the given string.
Input = “JAVA is the Best Programming Language in the Market”
Output = “Python is the Best Programming Language in the Market”

18). Write a Python program to get all the palindrome words from the string.
Input = “Python efe language aakaa hellolleh”
output = [“efe”, “aakaa”, “hellolleh”]

19). Write a Python program to create a string with a given list of words.
Input = [“There”, “are”, “Many”, “Programming”, “Language”]
Output = There are many programming languages.

20). Write a Python program to remove duplicate words from the string.
Input = “John jany sabi row john sabi”
output = “John jany sabi row”

21). Write a Python to remove unwanted characters from the given string.
Input = “Prog^ra*m#ming”
Output = “Programming”

Input = “Py(th)#@&on Pro$*#gram”
Output = “PythonProgram”

22). Write a Python program to find the longest capital letter word from the string.
Input = “Learning PYTHON programming is FUN”
Output = “PYTHON”

23). Write a Python program to get common words from strings.
Input String1 = “Very Good Morning, How are You”
Input String1 = “You are a Good student, keep it up”
Output = “You Good are”

24). Write a Python program to find the smallest and largest word in a given string.
Input = “Learning is a part of life and we strive”
Output = “a”, “Learning”

25). Check whether the given string is a palindrome (similar) or not.
Input= sqatoolssqatools
Output= Given string is not a palindrome

26). Write a program using python to reverse the words in a string.
Input= sqatools python
Output= slootaqs

27). Write a program to calculate the length of a string.
Input= “python”
Output = 6

28). Write a program to calculate the frequency of each character in a string.
Input = “sqatools”
Output = {‘s’:2, ‘q’:1, ‘a’: 1, ‘t’:1,‘o’:2, ‘l’:1, ‘s’:1}

29). Write a program to combine two strings into one.
Input: 
A = ’abc’
B = ’def’
Output = abcdef

30). Write a program to print characters at even places in a string.
Input = ‘sqatools’
Output = saol

31). Write a program to check if a string has a number or not.
Input = ‘python1’
Output = ‘Given string have a number’

32). Write a python program to count the number of vowels in a string.
Input= ‘I am learning python’
Output= 6

33). Write a python program to count the number of consonants in a string.
Input= ‘sqltools’
Output= 6

34). Write a program to print characters at odd places in a string.
Input = ‘abcdefg’
Output = ‘bdf’

35). Write a program to remove all duplicate characters from a given string in python.
Input = ‘sqatools’
Output = ‘sqatol’

36). Write a program to check if a string has a special character or not
Input = ‘python$$#sqatools’
Output =  ‘Given string has special characters

37). Write a program to exchange the first and last letters of the string
Input = We are learning python
Output = ne are learning pythoW

38). Write a program to convert all the characters in a string to Upper Case.
Input = ‘I live in pune’
Output = ‘I LIVE IN PUNE’

39). Write a program to remove a new line from a string using python.
Input = ‘objectorientedprogramming\n’
Output = ‘Objectorientedprogramming’

40). Write a python program to split and join a string
Input =‘Hello world’
Output = [‘Hello’, ‘world’]
                 Hello-world

41). Write a program to print floating numbers up to 3 decimal places and convert it to string.
Input = 2.14652
Output= 2.146

42). Write a program to convert numeric words to numbers.
Input = ‘five four three two one’
Output = 54321

43). Write a python program to find the location of a word in a string
Input Word = ‘problems’
Input string = ‘ I am solving problems based on strings’
Output = 4

44). Write a program to count occurrences of a word in a string.

Word = ‘food’
Input str = ‘ I want to eat fast food’
Occurrences output= 1

Word = ‘are’
Input str = “We are learning Python, wow are you”
Occurrences output = 2 

45). Write a python program to find the least frequent character in a string.
Input =  ‘abcdabdggfhf’
Output = ‘c’

46). Find the words greater than the given length.
Ex length = 3
Input = ‘We are learning python’
Output – ‘learning python’

47). Write a program to get the first 4 characters of a string.
Input = ‘Sqatools’
Output = ‘sqat’

48). Write a Python program to get a string made of the first 2 and the last 2 chars from a given string.
Input = ‘Sqatools’
Output = ‘Sqls’ 

49). Write a python program to print the mirror image of the string.
Input = ‘Python’
Output = ‘nohtyp 

50). Write a python program to split strings on vowels
Input = ‘qwerty’
Output = ‘qw rty’

51). Write a python program to replace multiple words with certain words.
Input = “I’m learning python at Sqatools”
Replace python with SQA  and sqatools with TOOLS 
Output = “I’m learning SQA at TOOLS “

52). Write a python program to replace different characters in the string at once.
Input = ‘Sqatool python’
Replace a with 1,
Replace t with 2,
Replace o with 3
Output = ‘sq1233l py2h3n”

53). Write a python program to remove empty spaces from a list of strings.
Input = [‘Python’, ‘ ‘, ‘ ‘, ‘sqatools’]
Output = [‘Python’, ‘sqatools’] 

54).  Write a python program to remove punctuations from a string
Input = ‘Sqatools : is best, for python’
Output = ‘Sqatools is best for python’

55).  Write a python program to find duplicate characters in a string
Input = “hello world”
Output = ‘lo’

56).  Write a python program to check whether the string is a subset of another string or not
Input str1 = “iamlearningpythonatsqatools”
str = ‘pystlmi’
Output = True

57). Write a python program to sort a string
Input = ‘xyabkmp’
Output = ‘abkmpxy’

58). Write a python program to generate a random binary string of a given length.
Input = 8
Output = 10001001

59). Write a python program to check if the substring is present in the string or not
Input string= ‘I live in Pune’
Substring= ‘I live ‘
Output = ‘Yes

60). Write a program to find all substring frequencies in a string.
Input str1 = “abab” 
Output = {‘a’: 2, ‘ab’: 2, ‘aba’: 1,‘abab’: 1, ‘b’: 2, ‘ba’: 1, ‘bab’: 1}  

61). Write a python program to print the index of the character in a string.
Input = ‘Sqatools’
Output = ‘The index of q is 1’

62). Write a program to strip spaces from a string.
Input = ‘    sqaltoolspythonfun     ‘ 
Output = ‘ sqaltoolspythonfun’

63). Write a program to check whether a string contains all letters of the alphabet or not.
Input = ‘abcdgjksoug’
Output = False

64). Write a python program to convert a string into a list of words.
Input = ‘learning python is fun’
Output = [learning, python, is, fun] 

65). Write a python program to swap commas and dots in a string.
Input = sqa,tools.python
Output = sqa.tools,python

66). Write a python program to count and display the vowels in a string
Input = ‘welcome to Sqatools’
Output = 7

67). Write a Python program to split a string on the last occurrence of the delimiter. 
Input = ‘l,e,a,r,n,I,n,g,p,y,t,h,o,n’
Output = [‘l,e,a,r,n,I,n,g,p,y,t,h,o ‘ ,’n’]

68). Write a Python program to find the first repeated word in a given string. 
Input = ‘ab bc ca ab bd’
Output = ‘ab’

69). Write a program to find the second most repeated word in a given string using python.
Input = ‘ab bc ac ab bd ac nk hj ac’
Output = (‘ab’, 2)

70). Write a Python program to remove spaces from a given string.
Input = ‘python at sqatools’
Output = ‘pythonatsqatools’

71). Write a Python program to capitalize the first and last letters of each word of a given string.
Input = ‘this is my first program’
Output = ‘ThiS IS MY FirsT PrograM’

72). Write a Python program to calculate the sum of digits of a given string.
Input = ’12sqatools78′
Output = 18

73). Write a Python program to remove zeros from an IP address. 
Input = 289.03.02.054
Output = 289.3.2.54

74). Write a program to find the maximum length of consecutive 0’s in a given binary string using python.
Input = 10001100000111
Output = 5 

75). Write a program to remove all consecutive duplicates of a given string using python.
Input = ‘xxxxyy’
Output = ‘xy’

76). Write a program to create strings from a given string. Create a string that consists of multi-time occurring characters in the said string using python.
Input = “aabbcceffgh”
Output = ‘abcf’

77). Write a Python program to create a string from two given strings combining uncommon characters of the said strings.  

Input string :
s1 = ‘abcdefg’
s2 = ‘xyzabcd’
Output string : ‘efgxyz’

78). Write a Python code to remove all characters except the given character in a string. 
Input = “Sqatools python”
Remove all characters except S
Output = ‘S’

79). Write a program to count all the Uppercase, Lowercase, special character and numeric values in a given string using python.
Input = ‘@SqaTools.lin’
Output:
Special characters: 1
Uppercase characters: 2
Lowercase characters: 8

80). Write a Python program to count a number of non-empty substrings of a given string.
Input a string = ‘sqatools12’
Number of substrings = 55

81). Write a program to remove unwanted characters from a given string using python.
Input = ‘sqa****too^^{ls’
Output = ‘Sqatools’

82). Write a program to find the string similarity between two given strings using python.
Input
Str1 = ‘Learning is fun in Sqatools’
Str2 = ‘Sqatools Online Learning Platform’

Output :
Similarity : 0.4

83). Write a program to extract numbers from a given string using python.
Input = ‘python 456 self learning 89’
Output = [456, 89]

84). Write a program to split a given multiline string into a list of lines using python.
Input =”’This string Contains
Multiple
Lines”’
Output = [‘This string Contains’, ‘Multiple’, ‘Lines’]

85). Write a program to add two strings as they are numbers using python.
Input :
a=’3′, b=’7′
Output  = ’10’

86). Write a program to extract name from a given email address using python.
Input = ‘student1@gmail.com’
Output = ‘student’

87). Write a  program that counts the number of leap years within the range of years using python. The range of years should be accepted as a string. 

(“1981-2001)  =  Total leap year 5

88). Write a program to insert space before every capital letter appears in a given word using python. 
Input = ‘SqaTools pyThon’
Output = ‘ Sqa Tools py Thon’ 

89). Write a program to uppercase half string using python.
Input = ‘banana’
Output = ‘banANA’

90). Write a program to split and join a string using “-“.
Input = ‘Sqatools is best’
Output = ‘Sqatools-is-best’

91). Write a python program to find permutations of a given string using in built function.
Input  = ‘CDE’
Output = [‘CDE’, ‘CED’ ‘EDC’, ‘ECD’, ‘DCE’, ‘DEC’]

92). Write a program to avoid spaces in string and get the total length
Input = ‘sqatools is best for learning python’
Output = 31

93). Write a program to accept a string that contains only vowels
Input = ‘python’
Output- ‘not accepted’

Input = ‘aaieou’
Output = ‘accepted’

94). Write a program to remove the kth element from the string
K=2
Input = ‘sqatools’
Output = ‘sqtools’

95). Write a program to check if a given string is binary or not.
Hint: Binary numbers only contain 0 or 1.

Input = ‘01011100’
Output = yes

Input = ‘sqatools 100’
Output = ‘No’

96). Write a program to add ‘ing’ at the end of the string using python.
Input = ‘xyz’
Output = ‘xyzing’

97). Write a program to add ly at the end of the string if the given string ends with ing.
Input = ‘winning’
Output = ‘winningly’

98). Write a program to reverse words in a string using python.
Input = ‘string problems’
Output = ‘problems string’

99). Write a program to print the index of each character in a string.
Input =  ‘sqatools’
Output :
Index of s is 0
Index of q is 1
Index of a is 2
Index of t is 3
Index of o is 4
Index of o is 5
Index of l is 6
Index of s is 7

100). Write a program to find the first repeated character in a string and its index.
Input = ‘sqatools’
Output = (s,0)

101). Write a program to swap cases of a given string using python.
Input = ‘Learning Python’
Output = ‘lEARNING pYTHON’

102). Write a program to remove repeated characters in a string and replace it with a single letter using python.
Input = ‘aabbccdd’
Output = ‘cabd’

103). Write a program to print a string 3 times using python.
Input = ‘sqatools’
Output = ‘sqatoolssqatoolssqatools’

104). Write a program to print each character on a new line using python.
Input = ‘python’
Output:
p
y
t
h
o
n

105). Write a program to get all the email id’s from given string using python.
Input str = “”” We have some employee whos john@gmail.com email id’s are randomly distributed jay@lic.com we want to get hari@facebook.com all the email mery@hotmail.com id’s from this given string.”””
Output = [‘john@gmail.com’, ‘ jay@lic.com’, ‘hari@facebook.com’, ‘mery@hotmail.com’ ]

106). Write a program to get a list of all the mobile numbers from the given string using python.
Input str = “”” We have 2233 some employee 8988858683 whos 3455 mobile numbers are randomly distributed 2312245566 we want 453452 to get 4532892234 all the mobile numbers 9999234355  from this given string.”””
Output = [‘8988858683’, ‘2312245566’, ‘4532892234’, ‘9999234355’]

Python Tuple Practice Programs, Exercises

Python tuple is a data structure that can hold any number of objects. A tuple is an immutable sequence of values

1). Python tuple program to create a tuple with 2 lists of data.
Input lists:
list1 = [4, 6, 8]
list2 = [7, 1, 4]
Output= ((4, 7), (6, 1), (8, 4))

2). Python tuple program to find the maximum value from a tuple.
Input = (41, 15, 69, 55)
Output = 69

3). Python tuple program to find the minimum value from a tuple.
Input = (36,5,79,25)
Output = 5

4). Python tuple program to create a list of tuples from a list having a number and its square in each tuple.
Input = [4,6,3,8]
Output = [ (4, 16), (6, 36), (3, 27), (8, 64) ]

5). Python tuple program to create a tuple with different datatypes.
Output= ( 2.6, 1, ‘Python’, True, [5, 6, 7], (5, 1, 4), {‘a’: 123, ‘b’: 456})

6). Python tuple program to create a tuple and find an element from it by its index no.
Input = (4, 8, 9, 1)
Index = 2
Output = 9

7). Python tuple program to assign values of tuples to several variables and print them.
Input = (6,7,3)
Variables = a,b,c
Output:
a, 6
b, 7
c, 3

8). Python tuple program to add an item to a tuple.
Input= ( 18, 65, 3, 45)
Output=(18, 65, 3, 45, 15)

9). Python tuple program to convert a tuple into a string.
Input = (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’, ‘s’)
Output = Sqatools

10). Python tuple program to get the 2nd element from the front and the 3rd element from the back of the tuple.
Input = (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’ ,’l’, ‘s’)
Output=
q
o

11). Python tuple program to check whether an element exists in a tuple or not.
Input = ( ‘p’ ,’y’, ‘t’, ‘h’, ‘o’, ‘n’)
P in A
Output=
True

12). Python tuple program to add a list in the tuple.
Input:
L=[12,67]
A=(6,8,4)
Output:
A=(6,8,4,12,67)

13). Python tuple program to find sum of elements in a tuple.
Input:
A=(4,6,2)
Output:
12

14). Python tuple program to add row-wise elements in Tuple Matrix
Input:
A = [[(‘sqa’, 4)], [(‘tools’, 8)]]
B = (3,6)
Output:
[[(‘sqa’, 4,3)], [(‘tools’, 8,6)]]

15). Python tuple program to create a tuple having squares of the elements from the list.
Input = [(1,5,7), (3,6)]
Output = (1, 81, 25, 49, 36)

16). Python tuple program to multiply adjacent elements of a tuple.
Input = (1,2,3,4)
Output =  (2,6,12)

17). Python tuple program to join tuples if the initial elements of the sub-tuple are the same.
Input:
[(3,6,7),(7,8,4),(7,3),(3,0,5)]
Output:
[(3,6,7,0,5),(7,8,4,3)]

18). Python tuple program to convert a list into a tuple and multiply each element by 2.
Input = [12,65,34,77]
Output = (24, 130, 68, 154)

19). Python tuple program to remove an item from a tuple.
Input:
A=(p,y,t,h,o,n)
Output: (p,y,t,o,n)

20). Python tuple program to slice a tuple.
Input:
A=(5,7,3,4,9,0,2)
Output:
(5,7,3)
(3,4,9)

21). Python tuple program to find an index of an element in a tuple.
Input:
A=(s,q,a,t,o,o,l,s)
Index of a?
Output = 2

22). Python tuple program to find the length of a tuple.
Input:
A=(v,i,r,a,t)
Output=
5

23). Python tuple program to convert a tuple into a dictionary.
Input:
A=((5,s),(6,l))
Output = { s: 5, l: 6 }

24). Python tuple program to reverse a tuple.
Input = ( 4, 6, 8, 3, 1)
Output= (1, 3, 8, 6, 4)

25). Python tuple program to convert a list of tuples in a dictionary.
Input = [ (s, 2), (q, 1), (a, 1), (s, 3), (q, 2), (a, 4) ]
Output ={ s: [ 2, 3 ], q: [ 1, 2 ], a: [ 1 ,4 ] }

26). Python tuple program to pair all combinations of 2 tuples.
Input :
A=(2,6)
B=(3,4)
Output
[ (2, 3), (2, 4), (6, 3), (6, 4), (3, 2), (3, 6), (4, 2), (4, 6) ]

27). Python tuple program to remove tuples of length i.
Input = [ (2, 5, 7), (3, 4), ( 8, 9, 0, 5) ]
i=2
Output= [ (2, 5, 7), ( 8, 9, 0, 5) ]

28). Python tuple program to remove tuples from the List having an element as None.
Input = [(None, 2), (None, None), (5, 4), (1,6,7)]
Output= { (5, 4), (1, 6, 7) }

29). Python tuple program to remove Tuples from the List having every element as None.
Input = [(None,), (None, None), (5, 4), (1,6,7),(None,1)]
Output = [(5, 4), (1,6,7),(None,1)]

30). Python tuple program to sort a list of tuples by the first item.
Input = [ (1, 5), (7, 8), (4, 0), (3, 6) ]
Output = [ (1, 5), (3, 6), (4, 0), (7, 8) ]

31). Python tuple program to sort a list of tuples by the second item.
Input = [ (1, 5), (7, 8), (4, 0), (3, 6) ]
Output = [ (4, 0), (1, 5), (3, 6), (7, 8) ]

32). Python tuple program to sort a list of tuples by the length of the tuple.
Input = [(4, 5, 6), ( 6, ), ( 2, 3), (6, 7, 8, 9, 0 ) ]
Output=
[(6,),(2,3),(4,5,6),(6,7,8,9,0)]

33). Python tuple program to calculate the frequency of elements in a tuple.
Input=
(a, b, c, d, b, a, b)
Output=
{ a:2, b:3, c:1, d:1 }

34). Python tuple program to filter out tuples that have more than 3 elements.
Input=
[ (1, 4), (4, 5, 6), (2, ), (7, 6, 8, 9), (3, 5, 6, 0, 1) ]
Output= [(7, 6, 8, 9), (3, 5, 6, 0, 1)]

35). Python tuple program to assign the frequency of tuples to each tuple.
Input=
[ (s,q), (t, o, o, l), (p, y), (s, q) ]
Output= {(‘s’, ‘q’): 2, (‘t’, ‘o’, ‘o’, ‘l’): 1, (‘p’, ‘y’): 1}

36). Python program to find values of tuples at ith index number.
Input=
[ (1, 2, 3), (6, 5, 4), (7, 6, 8), (9, 0, 1) ]
I = 3
Output= (9,0,1)

37). Python tuple program to test whether a tuple is distinct or not.
Input=
(1,2,3,4)
(3,4,5,4,6,3)
Output=
Tuple is distinct
Tuple is not distinct

38). Python tuple program to convert a tuple to string datatype.
Input=
A=(4,1,7,5)
Output=
The given tuple is (4,1,7,5)

39). Python tuple program to remove empty tuples from a list of tuples.
Input=
[ (”,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’), () ]
Output=
[ (‘a’, ‘b’), (‘a’,  ‘b’,  ‘c’),  (‘d’) ]

40). Python tuple program to sort a tuple by its float element.
Input=
[(3,5.6),(6,2.3),(1,1.8)]
Output=
[(1,1.8),(6,2.3),(3,5.6)]

41). Python tuple program to count the elements in a list if an element is a tuple.
Input=
[1,6,8,5,(4,6,8,0),5,4)]
Output=
4

42). Python tuple program to multiply all the elements in a tuple.
Input=
(5,4,3,1)
Output=
60

43). Python tuple program to convert a string into a tuple.
Input=
“Sqatools”
Output=
(S,q,a,t,o,o,l,s)

44). Python tuple program to convert a tuple of string values to a tuple of integer values.
Input=
( ‘4563’, ’68’, ‘1,’ )
Output=
( 4563, 68, 1)

45). Python tuple program to convert a given tuple of integers into a number.
Input=
(4, 5, 3, 8)
Output=
4538

46). Python tuple program to compute the element-wise sum of tuples.
Input=
(1, 6, 7)
(4, 3, 0)
(2, 6, 8)
Output=
(7, 15, 15)

47). Python tuple program to convert a given list of tuples to a list of lists.
Input=
A=[(1,5),(7,8),(4,0)]
Output =
[ [1, 5], [7, 8], [4, 0] ]

48). Python tuple program to find all the tuples that are divisible by a number.
Input=
[(10,5,15),(25,6,35),(20,10)]
Output=
[(10,5,15),(20,10)]

49). Python tuple program to find tuples having negative elements.
Input=
[ (1, 7), (-4, -5), (0, 6), (-1, 3) ]
Output=
[(-4,-5),(-1,3)]

50). Python tuple program to find the tuples with positive elements.
Input=
[ (1, 7), (-4, -5), (0, 6), (-1, 3) ]
Output=
[ (1, 7), (0, 6) ]

51). Python tuple program to remove duplicates from a tuple.
Input=
(6, 4, 9, 0, 2, 6, 1, 3, 4)
Output=
(6, 4, 9, 0, 2, 1, 3)

52). Python tuple program to extract digits from a list of tuples.
Input=
[ (6, 87, 7), (4, 53), (11, 28, 3) ]
Output=
[ 1, 2, 3, 4, 5, 6, 7, 8 ]

53). Python tuple program to multiply ith element from each tuple from a list of tuples.
Input=
[ (4, 8, 3), (3, 4, 0), (1, 6, 2) ]
i=1
Output=
192

54). Python tuple program to flatten a list of lists into a tuple.
Input=
[ [s], [q], [a], [t], [o], [o], [l], [s] ]
Output=
(s, q, a, t, o, o, l, s)

55). Python tuple program to flatten a tuple list into a string.
Input=
[ (s, q, a), (t, o), (o, l, s) ]
Output=
‘s q a t o o l s’

56). Python tuple program to convert a tuple into a list by adding the string after every element of the tuple.
Input=
A=(1, 2, 3, 4), b=’sqatools’
Output=
[1, “sqatools”, 2, “sqatools”, 3, “sqatools”, 4, “sqatools”]

57). Write a program to convert a tuple to tuple pair.
Input=
(1, 2, 3)
Output=
[ (1, 2), (1, 3) ]

59). Python tuple program to convert a list of lists to a tuple of tuples.
Input=
[ [‘sqatools’], [‘is’], [‘best’]]
Output=
( (‘sqatools’), (‘is), (‘best’) )

60). Python tuple program to extract tuples that are symmetrical with others from a list of tuples.
Input=
[ (a, b, c), (d, e), (c, b, a) ]
Output=
(a, b, c)

61). Python tuple program to return an empty set if no tuples are symmetrical.
Input=
[(1, 5, 7), (3, 4), (4, 9, 0)]
Output=
set()

62). Python tuple program to remove nested elements from a tuple.
Input=
( ‘s’, ‘q’, ‘a’, (‘t’, ‘o’, ‘o’, ‘l’, ‘s’), ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’ )
Output=
(‘s’, ‘q’, ‘a’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’ ,’t’)

63). Python tuple program to sort a tuple by the maximum value of a tuple.
Input=
[ (1, 5, 7), (3, 4, 2), (4, 9, 0) ]
Output=
[ (4, 9, 0), (1, 5, 7), (3, 4, 2) ]

64). Python tuple program to sort a list of tuples by the minimum value of a tuple.
Input=
[(1,5,7),(3,4,2),(4,9,0)]
Output=
[(4,9,0),(1,5,7),(3,4,2)]

65). Python tuple program to concatenate two tuples.
Input=
(‘s’,’q’,’a)
(‘t’,’o’,’o,’l’)
Output=
((‘s’,’q’,’a),(‘t’,’o’,’o,’l’))

66). Python tuple program to order tuples by external list.
Input=
a=[(‘very’,8),(‘i’,6),(‘am,5),(‘happy’,0)]
List=[‘i’,’am’,’very’,’happy’]
Output=
[(‘i’,6),(‘am’,5),(‘very’,8),(‘happy’,0)]

67). Python tuple program to find common elements between two lists of tuples.
Input=
A=[(1,5),(4,8),(3,9)]
B=[(9,3),(5,6),(5,1),(0,4)]
Output=
{(3,9),(1,5)}

68). Python tuple program to convert a binary tuple to an integer.
Input=
A=(1,0,0)
Output=
4
Explanation=
2^2+0+0=4

69). Python tuple program to count the total number of unique tuples.
Input=
[ (8, 9), (4, 7), (3, 6), (8, 9) ]
Output=
3

70). Python tuple program to calculate the average of the elements in the tuple.
Input=
(5, 3, 9, 6)
Output=
5.75

71). Python tuple program to swap tuples.
Input=
A=(7,4,9)
B=(3,)
Output=
A=(3,)
B=(7,4,9)

72). Python tuple program to check the type of the input and return True if the type is a tuple and False if it is not a tuple.
Input=
A=( 7, 4, 9, 2, 0 )
Output=
True

73). Python tuple program to find the last element of a tuple using negative indexing.
Input=
A=(‘p’,y’,’t’,’o’,’n’)
Output=
n

Python If else Practice Programs, Exercises

Python If else practice programs help beginners to get expertise in conditional programming login. The if-else statement is a conditional statement in programming. It executes a set of instructions if a particular condition is true, and another set of instructions if the condition is false.

1). Python program to check given number is divided by 3 or not.

2). If else program to get all the numbers divided by 3 from 1 to 30.

3). If else program to assign grades as per total marks.
marks > 40: Fail
marks 40 – 50: grade C
marks 50 – 60: grade B
marks 60 – 70: grade A
marks 70 – 80: grade A+
marks 80 – 90: grade A++
marks 90 – 100: grade Excellent
marks > 100: Invalid marks

4). Python program to check the given number divided by 3 and 5.

5). Python program to print the square of the number if it is divided by 11.

6). Python program to check given number is a prime number or not.

7). Python program to check given number is odd or even.

8). Python program to check a given number is part of the Fibonacci series from 1 to 10.

9). Python program to check authentication with the given username and password.

10). Python program to validate user_id in the list of user_ids.

11). Python program to print a square or cube if the given number is divided by 2 or 3 respectively.

12). Python program to describe the interview process.

13). Python program to determine whether a given number is available in the list of numbers or not.

14). Python program to find the largest number among three numbers.

15). Python program to check any person eligible to vote or not
age > 18+ : eligible
age < 18: not eligible

16). Python program to check whether any given number is a palindrome.
Input: 121
Output: palindrome

17). Python program to check if any given string is palindrome or not.
Input: ‘jaj’
output = palindrome

18). Python program to check whether a student has passed the exam. If marks are greater than 35 students have passed the exam.
Input = Enter marks: 45
Output = Pass

19). Python program to check whether the given number is positive or not.
Input = 20
Output = True

20). Python program to check whether the given number is negative or not.
Input = -45
Output = True

21). Python program to check whether the given number is positive or negative and even or odd.
Input = 26
Output = The given number is positive and even

22). Python program to print the largest number from two numbers.
Input:
25, 63
Output = 63

23). Python program to check whether a given character is uppercase or not.
Input = A
Output = The given character is an Uppercase

24). Python program to check whether the given character is lowercase or not.
Input = c
Output = True

25). Python program to check whether the given number is an integer or not.
Input = 54
Output = True

26). Python program to check whether the given number is float or not.
Input = 12.6
Output = True

27). Python program to check whether the given input is a string or not.
Input = ‘sqatools’
Output = True

28). Python program to print all the numbers from 10-15 except 13
Output:
10
11
12
14

29). Python program to find the electricity bill. According to the following conditions:
Up to 50 units rs 0.50/unit
Up to 100 units rs 0.75/unit
Up to 250 units rs 1.25/unit
above 250 rs 1.50/unit
an additional surcharge of 17% is added to the bill
Input = 350
Output = 438.75

30). Python program to check whether a given year is a leap or not.
Input = 2000
Output = The given year is a leap year

31). Python Python program to check whether the input number if a multiple of two print “Fizz” instead of the number and for the multiples of three print “Buzz”. For numbers that are multiples of both two and three print “FizzBuzz”.
Input = 14
Output = Fizz
Input = 9
Output = Buzz
Input = 6
Output = FizzBuzz

32). Python program to check whether an alphabet is a vowel.
Input = A
Output = True

33). Python program to check whether an alphabet is a consonant.
Input = B
Output = True

34).  Python program to convert the month name to the number of days.
Input = February
Output = 28/29 days

35). Python program to check whether a triangle is equilateral or not. An equilateral triangle is a triangle in which all three sides are equal.
Input:
Enter the length of the sides of the triangle
A=10
B=10
C=10
Output = True

36). Python program to check whether a triangle is scalene or not. A scalene triangle is a triangle that has three unequal sides.
Input:
Enter the length of the sides of the triangle
A=10
B=15
C=18
Output = True

37). Python program to check whether a triangle is isosceles or not. An isosceles triangle is a triangle with (at least) two equal sides.
Input:
Enter the length of the sides of the triangle
A=10
B=15
C=10
Output = True

38). Python program that reads month and returns season for that month.
Input = February
Output = Summer

39). Python program to check whether the input number is a float or not if yes then round up the number to 2 decimal places.
Input = 25.3614
Output = 25.36

40). Python program to check whether the input number is divisible by 12 or not.
Input = 121
Output = True

41). Python program to check whether the input number is a square of 6 or not.
Input = 37
Output = False

42). Python program to check whether the input number is a cube of 3 or not.
Input = 27
Output = True

43). Python program to check whether two numbers are equal or not.
Input:
A=26,B=88
Output = The given numbers are not equal

44). Python program to check whether the given input is a complex type or not.
Input:
a=5+6j
Output: True

45). Python program to check whether the given input is Boolean type or not.
Input:
a=True
Output = The given variable is Boolean

46). Python program to check whether the given input is List or not.
Input:
a=[1,3,6,8]
Output = True

47). Python program to check whether the given input is a dictionary or not.
Input:
A={‘name’:’Virat’,’sport’:’cricket’}
Output = True

48). Python program to check the eligibility of a person to sit on a roller coaster ride or not. Eligible when age is greater than 12.
Input = 15
Output = You are eligible

49). Python program to create 10 groups of numbers between 1-100 and find out given input belongs to which group using python nested if else statements.
Input= 36
Output = The given number belongs to 4th group

50). Python program to find employees eligible for bonus. A company decided to give a bonus of 10% to employees. If the employee has served more than 4 years. Ask the user for years served and check whether an employee is eligible for a bonus or not.
Input = Enter Years served: 5
Output = You are eligible for a bonus

51). Take values of the length and breadth of a rectangle from the user and check if it is square or not using the python if else statement.
Input:
Length= 4
Breadth= 5
Output = It is not a square

52). A shop will give a 10% discount if the bill is more than 1000, and 20% if the bill is more than 2000. Using the python program Calculate the discount based on the bill.
Input = 1500
Output = Discount amount: 150

53). Python program to print the absolute value of a number defined by the user.
Input = -1
Output = 1

54). Python program to check the student’s eligibility to attend the exam based on his/her attendance. If attendance is greater than 75% eligible if less than 75% not eligible.
Input = Enter attendance: 78
Output = You are eligible

55). Python program to check whether the last digit of a number defined by the user is divisible by 4 or not.
Input = 58
Output = The last digit is divisible by 4

56). Python program to display 1/0 if the user gives Hello/Bye as output.
Input = Enter your choice: Hello
Output = 1
Input = Enter your choice: Bye
Output = 0

57). Python program to accept the car price of a car and display the road tax to be paid according to the following criteria:
Cost price<500000 –> tax:15000
Cost price<1000000 –> tax:50000
Cost price<1500000 –> tax:80000
Input = Car Price: 1200000
Output = Tax payable: 50000

58). Using a python program take input from the user between 1 to 7 and print the day according to the number. 1 for Sunday 2 for Monday so on.
Input = Enter number: 7
Output = Saturday

59). Python program to accept the city name and display its monuments (take Pune and Mumbai as cities).
Input = Enter city name: Pune
Output:
Shaniwar vada
Lal mahal
Sinhgad fort

60). Python program to check whether the citizen is a senior citizen or not. An age greater than 60 than the given citizen is a senior citizen.
Input = Enter age: 70
Output = The given citizen is a senior citizen

61). Python program to find the lowest number between three numbers.
Input:
A=45
B=23
C=68
Output = 23

62). Python program to accept the temperature in Fahrenheit and check whether the water is boiling or not.
Hint: The boiling temperature of water in Fahrenheit is 212 degrees
Input = Enter temperature: 190
Output = Water is not boiling

63). Python program to accept two numbers and mathematical operations from users and perform mathematical operations according to it.
Input:
A=30
B=45
Operation = +
Output = 75

64). Python program to accept marks from the user allot the stream based on the following criteria.
Marks>85: Science
Marks>70: Commerce
35<Marks<70: Arts
Marks<35: Fail
Input = Marks: 88
Output = Science

Python List Programs, Exercises

Python list programs help beginners to become experts and the list is one of the most important data structures in Python Programming. Python list can contain any type of data as a member that we can access via positive and negative indexing.

In this article, we have two different sections one for Python list programs for beginners and the second for Python list projects for practice.

python list programs

Python List Programs

1). Python program to calculate the square of each number from the given list.

2). Python program to combine two lists.

3). Python program to calculate the sum of all elements from a list.

4). Python program to find a product of all elements from a given list.

5). Python program to find the minimum and maximum elements from the list.

6). Python program to differentiate even and odd elements from the given list.

7). Python program to remove all duplicate elements from the list.

8). Python program to print a combination of 2 elements from the list whose sum is 10.

9). Python program to print squares of all even numbers in a list.

10). Python program to split the list into two-part, the left side all odd values and the right side all even values.
Input = [5, 7, 2, 8, 11, 12, 17, 19, 22]
Output = [5, 7, 11, 17, 19, 2, 8, 12, 22]

11).  Python program to get common elements from two lists.
Input =
list1 = [4, 5, 7, 9, 2, 1]
list2 = [2, 5, 8, 3, 4, 7]
Outputt : [4, 5, 7, 2]

12). Python program to reverse a list with for loop.

13). Python program to reverse a list with a while loop.

14). Python program to reverse a list using index slicing.

15). Python program to reverse a list with reversed and reverse methods.

16). Python program to copy or clone one list to another list.

17). Python program to return True if two lists have any common member.

18). Python program to print a specific list after removing the 1st, 3rd, and 6th elements from the list.

19). Python program to remove negative values from the list.

20). Python program to get a list of all elements which are divided by 3 and 7.

21). Python program to check whether the given list is palindrome or not. (should be equal from both sides).

22). Python Program to get a list of words which has vowels in the given string.
Input: “www Student ppp are qqqq learning Python vvv”
Output : [‘Student’, ‘are’, ‘learning’, ‘Python’]

23). Python program to add 2 lists with extend method.

24). Python program to sort list data, with the sort and sorted method.

25). Python program to remove data from the list from a specific index using the pop method.

26). Python program to get the max, min, and sum of the list using in-built functions.

27). Python program to check whether a list contains a sublist.

28). Python program to generate all sublists with 5 or more elements in it from the given list.

29). Python program to find the second largest number from the list.
 

30). Python program to find the second smallest number from the list.

31). Python program to merge all elements of the list in a single entity using a special character.
 

32). Python program to get the difference between two lists.

33). Python program to reverse each element of the list.
Input = [‘Sqa’, ‘Tools’, ‘Online’, ‘Learning’, ‘Platform’]
output = [‘aqS’, ‘slooT’, ‘enilno’, ‘gninraeL’, ‘mroftalP’]
34). Python program to combine two list elements as a sublist in a list.
list1 = [3, 5, 7, 8, 9]
list2 = [1, 4, 3, 6, 2]
Output = [[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]
35). Python program to get keys and values from the list of dictionaries.
Input : [{‘a’:12}, {‘b’: 34}, {‘c’: 23}, {‘d’: 11}, {‘e’: 15}]
Output :  [‘a’, ‘b’, ‘c’, ‘d’, ‘c’]
                [12, 34, 23, 11, 15]

36). Python program to get all the unique numbers in the list.

37). Python program to convert a string into a list.

38). Python program to replace the last and the first number of the list with the word.
Input: [12, 32, 33, 5, 4, 7]
output : [‘SQA’, 32, 33, 5, 4, ‘Tools’]
 

39). Python program to check whether the given element is exist in the list or not.

40). Python program to remove all odd index elements.
Input: [12, 32, 33, 5, 4, 7, 33]
Output: [12,33,4,33]

41). Python program to take two lists and return true if then at least one common member.

42). Python program to convert multiple numbers from a list into a single number.
Input: [12, 45, 56]
Output:124556
43). Python program to convert words of a list into a single string.
Input: [‘Sqa’, ‘Tools’, ‘Best’, ‘Learning’, ‘Platform’]
Output: SqaToolsBestLearningPlatform
44). Python program to print elements of the list separately.
Input: [(‘Black’, ‘Yellow’, ‘Blue’), (50, 55, 60), (30.0, 50.5, 55.66)]
Output:
(‘Black’, ‘Yellow’, ‘Blue’)
(50, 55, 60)
(30.0, 50.5, 55.66)
45). Python program to create a sublist of numbers and their squares from 1 to 10.
Output : [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81], [10, 100]]
46). Python program to create a list of five consecutive numbers in the list.
Output : [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]
47). Python program to insert a given string at the beginning of all items in a list.
Input: [1, 2, 3, 4, 5], Sqa
Output: [‘Sqa1’, ‘Sqa2’, ‘Sqa3’, ‘Sqa4’, ‘Sqa5’]
48). Python program to iterate over two lists simultaneously and create a list of sublists.
list1 = [1, 3, 5, 7, 9]
list2 = [8, 6, 4, 2, 10]
output = [[1, 8], [3, 6], [5, 4], [7, 2], [9, 10]]
49). Python program to move all positive numbers on the left side and negative numbers on the right side.
Input: [2, -4, 6, 44, -7, 8, -1, -10]
Output: [2, 6, 44, 8, -4, -7, -1, -10]
50). Python program to move all zero digits to the end of a given list of numbers.
Input: [3, 4, 0, 0, 0, 0, 6, 0, 4, 0, 22, 0, 0, 3, 21, 0]
Output: [3, 4, 6, 4, 22, 3, 21, 0, 0, 0, 0, 0, 0, 0, 0]
51). Python program to find the list in a list of lists whose sum of elements is the highest.
Input: [[11, 2, 3], [4, 15, 6], [10, 11, 12], [7 8, 19]]
Output: [7, 8, 19]
52). Python program to find the items that start with a specific character from a given list.
Input: [‘abbcd’, ‘ppq, ‘abdd’, ‘agr’, ‘bhr’, ‘sqqa’, tools, ‘bgr’]
 
# item starts with a from the given list.
[‘abbcd’, ‘abdd’, ‘agr’]
 
# item starts with b from the given list.
[‘bhr’, ‘bgr’]
 
# item starts with c from the given list.
[]
53). Python program to count empty dictionaries from the given list.
Input: [{}, {‘a’: ‘sqatools’}, [], {}, {‘a’: 123}, {},{},()]
empty_count: 3
54). Python program to remove consecutive duplicates of given lists.
Input: [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]
Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 4]
55). Python program to pack consecutive duplicates of given list elements into sublists.
Input: [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9]
Output: [[0, 0], [1], [2], [3], [4, 4], [5], [6, 6], [7], [8, 8], [9]]
56). Python program to split a given list into two parts where the length of the first part of the list is given.
Input: [4, 6, 7, 3, 2, 5, 6, 7, 6, 4]
length of the first part is 4
Output: [[4, 6, 7, 3], [2, 5, 6, 7, 6, 4]]
57). Python program to insert items at a specific position in the list.
Input: [2, 4, 6, 8, 3, 22]
Index: 3
Item: 55
Output: [2, 4, 6, 55, 8, 3, 22]
58). Python program to select random numbers from the list.
Input: [1, 4, 5, 7, 3, 2, 9]
Selected 4 random numbers from the list.
59). Python program to create a 3*3 grid with numbers.
Output: [[4, 5, 6], [4, 5, 6], [4, 5, 6]]
60). Python program to zip two lists of lists into a list.
list1: [[1, 3], [5, 7], [9, 11]]
list2: [[2, 4], [6, 8], [10, 12, 14]]
61). Python program to convert the first and last letter of each item from Upper case and lowercase.
Input: [‘Learn’, ‘python’, ‘From’, ‘Sqa’, tools]
Output =
[‘LearN ‘, ‘PythoN ‘, ‘FroM ‘, ‘SqA ‘, ‘ToolS ‘]
[‘learn ‘, ‘python ‘, ‘from ‘, ‘sqa ‘, ‘tools ‘]
62). Python to find maximum and minimum values in the given heterogeneous list.
Input: [‘Sqa’, 6, 5, 2, ‘Tools’]
Output: [6,2]
63). Python program to sort a given list in ascending order according to the sum of its sublist.
Input: [[3, 5, 6], [2, 1, 3], [5, 1, 1], [1, 2, 1], [0, 4, 1]]
            14         6         7           4          5
Output = [[1, 2, 1], [0, 4, 1], [2, 1, 3], [5, 1, 1], [3, 5, 6]]
64). Python program to extract the specified sizes of strings from a given list of string values.
Input: [‘Python’, ‘Sqatools’, ‘Practice’, ‘Program’, ‘test’, ‘list’]
size = 8
Output: [‘Sqatools’, ‘Practice’]
65). Python program to find the difference between consecutive numbers in a given list.
Input list: [1, 1, 3, 4, 4, 5, 6, 7]
Output list: [0, 2, 1, 0, 1, 1, 1]
66). Python program to calculate the average of the given list.
Input : [3, 5, 7, 2, 6, 12, 3]
Output: 5.428571428571429
67). Python program to count integers in a given mixed list.
Input list: [‘Hello’, 45, ‘sqa’,  23, 5, ‘Tools’, 20]
Output: 4
68). Python program to access multiple elements of the specified index from a given list.
Input list: [2, 3, 4, 7, 8, 1, 5, 6, 2, 1, 8, 2]
Index list: [0, 3, 5, 6]
Output: [2, 7, 1, 5]
69). Python program to check whether a specified list is sorted or not.
Input list : [1, 2, 3, 5, 7, 8, 9]
Output: True
 
Input list: [3, 5, 1, 6, 8, 2, 4]
Output: False
70). Python program to remove duplicate dictionaries from a given list.
Input : [{‘name’: ‘john’}, {‘city’: ‘mumbai’}, {‘Python’: ‘laguage’}, {‘name’: ‘john’}]
Output: [{‘city’: ‘mumbai’}, {‘Python’: ‘laguage’}]
71). Python program to check if the elements of a given list are unique or not.
Input: [2, 5, 6, 7, 4, 11, 2, 4, 66, 21, 22, 3]
Output: False
72). Python program to remove duplicate sublists from the list.
Input: [[1, 2], [3, 5], [1, 2], [6, 7]]
Output: [[3, 5],[6, 7]]
73). Python program to create a list by taking an alternate item from the list.
Input: [3, 5, 7, 8, 2, 9, 3, 5, 11]
Output: [3, 7, 2, 3, 11]
74). Python program to remove duplicate tuples from the list.
Input: [(2, 3), (4, 6), (5, 1), (2, 3), (7, 9), (5, 1)]
Output: [(2,3), (4, 6), (5,1), (7, 9)]
75). Python program to insert an element before each element of a list.
Input :[3, 5, 7, 8]
element = ‘a’
Output: [‘a’, 3, ‘a’, 5, ‘a’, 7, ‘a’, 8]
76). Python program to remove the duplicate string from the list.
Input: [‘python’, ‘is’, ‘a’, ‘best’, ‘language’, ‘python’, ‘best’]
Output: [‘python’, ‘is’, ‘a’, ‘best’, ‘language’]

77). Python program to get the factorial of each item in the list.

78). Python program to get a list of Fibonacci numbers from 1 to 20.

79). Python program to reverse all the numbers in a given list.
Input : [123, 145, 633, 654, 254]
Output: [321, 541, 336, 456, 452]
80). Python program to get palindrome numbers from a given list.
Input : [121, 134, 354, 383, 892, 232]
Output : [121, 282, 232]
81). Python program to get a count of vowels in the given list.
Input : [‘Learning’, ‘Python’, ‘From’, ‘SqaTool’]
Output : 8
82). Python program to get the list of prime numbers in a given list.
Input : [11, 8, 7, 19, 6, 29]
Output : [11,  7,  19, 29]
83). Python program to get a list with n elements removed from the left and right.
Input : [2, 5, 7, 9, 3, 4]
Remove 1 element from left
[5, 7, 9, 3, 4]
 
Remove 1 element from the right
[2, 5, 7, 9, 3]
 
Remove 2 elements from left
[7, 9, 3, 4]
 
Remove 2 elements from right
[2, 5, 7, 9]
84). Python program to create a dictionary with two lists.
Input :
list1 : [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
list2 : [234, 123, 456, 343, 223]
Output: {‘a’: 234, ‘b’: 123, ‘c’: 456, ‘d’: 343, ‘e’: 223}
85). Python program to remove the duplicate item from the list using set.
Input : [2, 5, 7, 8, 2, 3, 4, 12, 5, 6]
Output : [2, 5, 7, 8, 3, 4, 12, 6]
86). Python program to insert a sublist into the list at a specific index.
Input : [4, 6, 8, 2, 3, 5]
sublist, index
[5, 2, 6], 3
Output: [4, 6, 8, [5, 2, 6], 2, 3, 5]
87). Python program to calculate the bill per fruit purchased from a given fruits list.
Input =
Fruit list with Price: [[‘apple’, 30], [‘mango’, 50], [‘banana’, 20], [‘lichi’, 50]]
Fruit with quantity: [[‘apple’, 2]]
Output =
Fruit: Apple
Bill: 60
Fruit: mango
Bill: 500
88). Python program to calculate percentage from a given mark list, the max mark for each item is 100.
Marks_list : [80, 50, 70, 90, 95]
Output: 77%
89). Python program to get the list of all palindrome strings from the given list.
Input: [‘data’, ‘python’, ‘oko’, ‘test’, ‘ete’]
Output: [‘oko’, ‘ete’]
90). Python program to flatten a given nested list structure.
Input: [0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122]]
Output: [0, 12, 22, 32, 42, 52, 62, 72, 82, 92, 102, 112, 122]
91). Python program to convert tuples in the list into a sublist.
Input: [(3, 5), (6, 8), (8, 11), (12, 14), (17, 23)]
Output: [[3, 5], [6, 8], [8, 11], [12, 14], [17, 23]]
92). Python program to create a dictionary from a sublist in a given list.
Input: [[‘a’, 5], [‘b’, 8], [‘c’, 11], [‘d’, 14], [‘e’, 23]]
Output: {‘a’: 5, ‘b’: 8, ‘c’: 11, ‘d’: 14, ‘e’: 23}
93). Python program to replace ‘Java’ with ‘Python’ from the given list.
Input: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Python’, ‘Its’, ‘Python’, ‘Language’]
Output: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Java’, ‘Its’, ‘Java’, ‘Language’]
94). Python program to convert the 3rd character of each word to a capital case from the given list.
Input: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Python’, ‘Its’, ‘Python’, ‘Language’]
Output: [‘HelLo’, ‘stuDent’, ‘are’, ‘leaRning’, ‘PytHon’, ‘Its’, ‘PytHon’, ‘LanGuage’]
95). Python program to remove the 2nd character of each word from a given list.
Input: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Python’, ‘Its’, ‘Python’, ‘Language’]
Output[‘Hllo’, ‘sudent’, ‘ae’, ‘larning’, ‘Pthon’, ‘Is’, ‘Pthon’, ‘Lnguage’]
96). Python program to get a length of each word and add it as a dictionary from the given list.
Input: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Python’, ‘Its’, ‘Language’]
Output: [{‘Hello’:5, ‘student’: 7, ‘are’: 3, ‘learning’: 8, ‘Python’: 6, ‘Its’: 3, ‘Language’: 8}]
97). Python program to remove duplicate dictionaries from the given list.
Input: [{‘Hello’:5}, {‘student’: 7}, {‘are’: 3}, {‘learning’: 8}, {‘Hello’:5}, ,{‘Language’: 8}, {‘are’: 3}]
Output: [{‘Hello’:5, ‘student’: 7, ‘are’: 3, ‘learning’: 8, ‘Python’: 6, ‘Its’: 3, ‘Language’: 8}]
98). Python program to decode a run-length encoded given list.
Input: [[2, 1], 2, 3, [2, 4], 5, 1]
Output: [1, 1, 2, 3, 4, 4, 5, 1]
99). Python program to round every number in a given list of numbers and print the total sum of the list.
Input: [22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]
Output: 27
 
list1 = [2, 4, 6, 7, 5, 8, 3, 11]

result = [x**2 for x in list1 if x%2 == 0]

print("Result :", result)

Output :

Result : [4, 16, 36, 64]

100).  Python Program to get the Median of all the elements from the list.
list1 = [2, 4, 6, 7, 5, 8, 3, 11]

result = [x**2 for x in list1 if x%2 == 0]

print("Result :", result)

Output :

Result : [4, 16, 36, 64]

101). Python Program to get the Standard deviation of the list element.
102). Python program to convert all numbers to binary format from a given list.
103). Python program to convert all the numbers into Roman numbers from the given list.

Python Lists Project Ideas

1). To-do List: Python list program that allows users to create and manage their to-do lists. Users can add, delete, and update tasks on their list.  Each task can add along with a unique id which will first element of each sub-list through which the user can easily identify the item and task.

2). Shopping List: A program that allows users to create and manage their shopping list. Users can add, delete, and update items on their list. There are many Python list programs listed above those can help user to solve this project.

3). Game Leaderboard: A program that keeps track of scores for multiple players in a game. Use Python lists to store the scores and display a leaderboard of the top players. Nowadays people like to play games and users can apply a basic understanding of Python list programs to write code for this mini-project. 

4). Student Gradebook: A Python list program that allows teachers to enter and manage student grades. Use Python lists and sub-lists to store student names and their corresponding grades.

5). Password Manager: A program that securely stores and manages passwords using Python lists. Users can add, delete, and update passwords, and the program can encrypt the data for added security, Its good exercises to apply understanding Python list programs. 

6). Music Library: A Python list program that allows users to create and manage a music library. Users can add, delete, and update songs, artists, and album details in the list.

7). Recipe Book: A program that allows users to create and manage their recipe book. Users can add, delete, and update recipes, ingredients, and cooking instructions.

8). Contact List: A Python list program that allows users to manage their contact list. Users can add, delete, and update contacts’ names, phone numbers, and email addresses in the list.

9). Movie Library: A Python list program that allows users to create and manage a movie library. Users can add, delete, and update movies, actors, and directors in the list.

10). Sports Team Roster: A program that allows coaches to manage their sports team roster. Coaches can add, delete, and update players’ names, positions, and statistics in the list.

Official reference for python list data structure

Python String Programs, Exercises

Python string is a sequence of characters, such as “hello”. They can be used to represent text in a programming language. Strings can be created by enclosing a sequence of characters between double quotes, such as “hello”, or by using the str() function.

1). Write a Python program to get a string made of the first and the last 2 chars from a given string. If the string length is less than 2, return instead of the empty string

2). Python string program that takes a list of strings and returns the length of the longest string.

3). Python string program to get a string made of 4 copies of the last two characters of a specified string (length must be at least 2).

4). Python string program to reverse a string if it’s length is a multiple of 4.

5). Python string program to count occurrences of a substring in a string.

6). Python string program to test whether a passed letter is a vowel or consonant.

7). Find the longest and smallest word in the input string.

8). Print most simultaneously repeated characters in the input string.

9). Write a Python program to calculate the length of a string with loop logic.

10). Write a Python program to replace the second occurrence of any char with the special character $.
Input = “Programming”
Output = “Prog$am$in$”

11). Write a python program to get to swap the last character of a given string.
Input = “SqaTool”
Output = “IqaTooS”

12). Write a python program to exchange the first and last character of each word from the given string.
Input = “Its Online Learning”
Output = “stI enlino gearninL”

13). Write a python to count vowels from each word in the given string show as dictionary output.
Input = “We are Learning Python Codding”
output = {“We” : 1, “are” : 2, “Learning” : 3, “Python”:1, “Codding”}

14). Write a python to repeat vowels 3 times and consonants 2 times.
Input = “Sqa Tools Learning”
Ouput = “SSqqaaa TToooooollss LLeeeaaarrnniiinngg”

15). Write a python program to re-arrange the string.
Input = “Cricket Plays Virat”
Output = “Virat Plays Cricket”

16). Write a python program to get all the digits from the given string.
Input = “””
Sinak’s 1112 aim is to 1773 create a new generation of people who
understand 444 that an organization’s 5324 success or failure is
based on 555 leadership excellence and not managerial
acumen
“””
Output = [1112, 5324, 1773, 5324, 555]

17). Write a python program to replace the words “Java” with “Python” in the given string.
Input = “JAVA is the Best Programming Language in the Market”
Output = “Python is the Best Programming Language in the Market”

18). Write a Python program to get all the palindrome words from the string.
Input = “Python efe language aakaa hellolleh”
output = [“efe”, “aakaa”, “hellolleh”]

19). Write a Python program to create a string with a given list of words.
Input = [“There”, “are”, “Many”, “Programming”, “Language”]
Output = There are many programming languages.

20). Write a Python program to remove duplicate words from the string.
Input = “John jany sabi row john sabi”
output = “John jany sabi row”

21). Write a Python to remove unwanted characters from the given string.
Input = “Prog^ra*m#ming”
Output = “Programming”

Input = “Py(th)#@&on Pro$*#gram”
Output = “PythonProgram”

22). Write a Python program to find the longest capital letter word from the string.
Input = “Learning PYTHON programming is FUN”
Output = “PYTHON”

23). Write a Python program to get common words from strings.
Input String1 = “Very Good Morning, How are You”
Input String1 = “You are a Good student, keep it up”
Output = “You Good are”

24). Write a Python program to find the smallest and largest word in a given string.
Input = “Learning is a part of life and we strive”
Output = “a”, “Learning”

25). Check whether the given string is a palindrome (similar) or not.
Input= sqatoolssqatools
Output= Given string is not a palindrome

26). Write a program using python to reverse the words in a string.
Input= sqatools python
Output= slootaqs

27). Write a program to calculate the length of a string.
Input= “python”
Output = 6

28). Write a program to calculate the frequency of each character in a string.
Input = “sqatools”
Output = {‘s’:2, ‘q’:1, ‘a’: 1, ‘t’:1,‘o’:2, ‘l’:1, ‘s’:1}

29). Write a program to combine two strings into one.
Input: 
A = ’abc’
B = ’def’
Output = abcdef

30). Write a program to print characters at even places in a string.
Input = ‘sqatools’
Output = saol

31). Write a program to check if a string has a number or not.
Input = ‘python1’
Output = ‘Given string have a number’

32). Write a python program to count the number of vowels in a string.
Input= ‘I am learning python’
Output= 6

33). Write a python program to count the number of consonants in a string.
Input= ‘sqltools’
Output= 6

34). Write a program to print characters at odd places in a string.
Input = ‘abcdefg’
Output = ‘bdf’

35). Write a program to remove all duplicate characters from a given string in python.
Input = ‘sqatools’
Output = ‘sqatol’

36). Write a program to check if a string has a special character or not
Input = ‘python$$#sqatools’
Output =  ‘Given string has special characters

37). Write a program to exchange the first and last letters of the string
Input = We are learning python
Output = ne are learning pythoW

38). Write a program to convert all the characters in a string to Upper Case.
Input = ‘I live in pune’
Output = ‘I LIVE IN PUNE’

39). Write a program to remove a new line from a string using python.
Input = ‘objectorientedprogramming\n’
Output = ‘Objectorientedprogramming’

40). Write a python program to split and join a string
Input =‘Hello world’
Output = [‘Hello’, ‘world’]
                 Hello-world

41). Write a program to print floating numbers up to 3 decimal places and convert it to string.
Input = 2.14652
Output= 2.146

42). Write a program to convert numeric words to numbers.
Input = ‘five four three two one’
Output = 54321

43). Write a python program to find the location of a word in a string
Input Word = ‘problems’
Input string = ‘ I am solving problems based on strings’
Output = 4

44). Write a program to count occurrences of a word in a string.

Word = ‘food’
Input str = ‘ I want to eat fast food’
Occurrences output= 1

Word = ‘are’
Input str = “We are learning Python, wow are you”
Occurrences output = 2 

45). Write a python program to find the least frequent character in a string.
Input =  ‘abcdabdggfhf’
Output = ‘c’

46). Find the words greater than the given length.
Ex length = 3
Input = ‘We are learning python’
Output – ‘learning python’

47). Write a program to get the first 4 characters of a string.
Input = ‘Sqatools’
Output = ‘sqat’

48). Write a Python program to get a string made of the first 2 and the last 2 chars from a given string.
Input = ‘Sqatools’
Output = ‘Sqls’ 

49). Write a python program to print the mirror image of the string.
Input = ‘Python’
Output = ‘nohtyp 

50). Write a python program to split strings on vowels
Input = ‘qwerty’
Output = ‘qw rty’

51). Write a python program to replace multiple words with certain words.
Input = “I’m learning python at Sqatools”
Replace python with SQA  and sqatools with TOOLS 
Output = “I’m learning SQA at TOOLS “

52). Write a python program to replace different characters in the string at once.
Input = ‘Sqatool python’
Replace a with 1,
Replace t with 2,
Replace o with 3
Output = ‘sq1233l py2h3n”

53). Write a python program to remove empty spaces from a list of strings.
Input = [‘Python’, ‘ ‘, ‘ ‘, ‘sqatools’]
Output = [‘Python’, ‘sqatools’] 

54).  Write a python program to remove punctuations from a string
Input = ‘Sqatools : is best, for python’
Output = ‘Sqatools is best for python’

55).  Write a python program to find duplicate characters in a string
Input = “hello world”
Output = ‘lo’

56).  Write a python program to check whether the string is a subset of another string or not
Input str1 = “iamlearningpythonatsqatools”
str = ‘pystlmi’
Output = True

57). Write a python program to sort a string
Input = ‘xyabkmp’
Output = ‘abkmpxy’

58). Write a python program to generate a random binary string of a given length.
Input = 8
Output = 10001001

59). Write a python program to check if the substring is present in the string or not
Input string= ‘I live in Pune’
Substring= ‘I live ‘
Output = ‘Yes

60). Write a program to find all substring frequencies in a string.
Input str1 = “abab” 
Output = {‘a’: 2, ‘ab’: 2, ‘aba’: 1,‘abab’: 1, ‘b’: 2, ‘ba’: 1, ‘bab’: 1}  

61). Write a python program to print the index of the character in a string.
Input = ‘Sqatools’
Output = ‘The index of q is 1’

62). Write a program to strip spaces from a string.
Input = ‘    sqaltoolspythonfun     ‘ 
Output = ‘ sqaltoolspythonfun’

63). Write a program to check whether a string contains all letters of the alphabet or not.
Input = ‘abcdgjksoug’
Output = False

64). Write a python program to convert a string into a list of words.
Input = ‘learning python is fun’
Output = [learning, python, is, fun] 

65). Write a python program to swap commas and dots in a string.
Input = sqa,tools.python
Output = sqa.tools,python

66). Write a python program to count and display the vowels in a string
Input = ‘welcome to Sqatools’
Output = 7

67). Write a Python program to split a string on the last occurrence of the delimiter. 
Input = ‘l,e,a,r,n,I,n,g,p,y,t,h,o,n’
Output = [‘l,e,a,r,n,I,n,g,p,y,t,h,o ‘ ,’n’]

68). Write a Python program to find the first repeated word in a given string. 
Input = ‘ab bc ca ab bd’
Output = ‘ab’

69). Write a program to find the second most repeated word in a given string using python.
Input = ‘ab bc ac ab bd ac nk hj ac’
Output = (‘ab’, 2)

70). Write a Python program to remove spaces from a given string.
Input = ‘python at sqatools’
Output = ‘pythonatsqatools’

71). Write a Python program to capitalize the first and last letters of each word of a given string.
Input = ‘this is my first program’
Output = ‘ThiS IS MY FirsT PrograM’

72). Write a Python program to calculate the sum of digits of a given string.
Input = ’12sqatools78′
Output = 18

73). Write a Python program to remove zeros from an IP address. 
Input = 289.03.02.054
Output = 289.3.2.54

74). Write a program to find the maximum length of consecutive 0’s in a given binary string using python.
Input = 10001100000111
Output = 5 

75). Write a program to remove all consecutive duplicates of a given string using python.
Input = ‘xxxxyy’
Output = ‘xy’

76). Write a program to create strings from a given string. Create a string that consists of multi-time occurring characters in the said string using python.
Input = “aabbcceffgh”
Output = ‘abcf’

77). Write a Python program to create a string from two given strings combining uncommon characters of the said strings.  

Input string :
s1 = ‘abcdefg’
s2 = ‘xyzabcd’
Output string : ‘efgxyz’

78). Write a Python code to remove all characters except the given character in a string. 
Input = “Sqatools python”
Remove all characters except S
Output = ‘S’

79). Write a program to count all the Uppercase, Lowercase, special character and numeric values in a given string using python.
Input = ‘@SqaTools.lin’
Output:
Special characters: 1
Uppercase characters: 2
Lowercase characters: 8

80). Write a Python program to count a number of non-empty substrings of a given string.
Input a string = ‘sqatools12’
Number of substrings = 55

81). Write a program to remove unwanted characters from a given string using python.
Input = ‘sqa****too^^{ls’
Output = ‘Sqatools’

82). Write a program to find the string similarity between two given strings using python.
Input
Str1 = ‘Learning is fun in Sqatools’
Str2 = ‘Sqatools Online Learning Platform’

Output :
Similarity : 0.4

83). Write a program to extract numbers from a given string using python.
Input = ‘python 456 self learning 89’
Output = [456, 89]

84). Write a program to split a given multiline string into a list of lines using python.
Input =”’This string Contains
Multiple
Lines”’
Output = [‘This string Contains’, ‘Multiple’, ‘Lines’]

85). Write a program to add two strings as they are numbers using python.
Input :
a=’3′, b=’7′
Output  = ’10’

86). Write a program to extract name from a given email address using python.
Input = ‘student1@gmail.com’
Output = ‘student’

87). Write a  program that counts the number of leap years within the range of years using python. The range of years should be accepted as a string. 

(“1981-2001)  =  Total leap year 5

88). Write a program to insert space before every capital letter appears in a given word using python. 
Input = ‘SqaTools pyThon’
Output = ‘ Sqa Tools py Thon’ 

89). Write a program to uppercase half string using python.
Input = ‘banana’
Output = ‘banANA’

90). Write a program to split and join a string using “-“.
Input = ‘Sqatools is best’
Output = ‘Sqatools-is-best’

91). Write a python program to find permutations of a given string using in built function.
Input  = ‘CDE’
Output = [‘CDE’, ‘CED’ ‘EDC’, ‘ECD’, ‘DCE’, ‘DEC’]

92). Write a program to avoid spaces in string and get the total length
Input = ‘sqatools is best for learning python’
Output = 31

93). Write a program to accept a string that contains only vowels
Input = ‘python’
Output- ‘not accepted’

Input = ‘aaieou’
Output = ‘accepted’

94). Write a program to remove the kth element from the string
K=2
Input = ‘sqatools’
Output = ‘sqtools’

95). Write a program to check if a given string is binary or not.
Hint: Binary numbers only contain 0 or 1.

Input = ‘01011100’
Output = yes

Input = ‘sqatools 100’
Output = ‘No’

96). Write a program to add ‘ing’ at the end of the string using python.
Input = ‘xyz’
Output = ‘xyzing’

97). Write a program to add ly at the end of the string if the given string ends with ing.
Input = ‘winning’
Output = ‘winningly’

98). Write a program to reverse words in a string using python.
Input = ‘string problems’
Output = ‘problems string’

99). Write a program to print the index of each character in a string.
Input =  ‘sqatools’
Output :
Index of s is 0
Index of q is 1
Index of a is 2
Index of t is 3
Index of o is 4
Index of o is 5
Index of l is 6
Index of s is 7

100). Write a program to find the first repeated character in a string and its index.
Input = ‘sqatools’
Output = (s,0)

101). Write a program to swap cases of a given string using python.
Input = ‘Learning Python’
Output = ‘lEARNING pYTHON’

102). Write a program to remove repeated characters in a string and replace it with a single letter using python.
Input = ‘aabbccdd’
Output = ‘cabd’

103). Write a program to print a string 3 times using python.
Input = ‘sqatools’
Output = ‘sqatoolssqatoolssqatools’

104). Write a program to print each character on a new line using python.
Input = ‘python’
Output:
p
y
t
h
o
n

105). Write a program to get all the email id’s from given string using python.
Input str = “”” We have some employee whos john@gmail.com email id’s are randomly distributed jay@lic.com we want to get hari@facebook.com all the email mery@hotmail.com id’s from this given string.”””
Output = [‘john@gmail.com’, ‘ jay@lic.com’, ‘hari@facebook.com’, ‘mery@hotmail.com’ ]

106). Write a program to get a list of all the mobile numbers from the given string using python.
Input str = “”” We have 2233 some employee 8988858683 whos 3455 mobile numbers are randomly distributed 2312245566 we want 453452 to get 4532892234 all the mobile numbers 9999234355  from this given string.”””
Output = [‘8988858683’, ‘2312245566’, ‘4532892234’, ‘9999234355’]