STR5NG
[An editor is available at the bottom of the page to write and execute the scripts.]
1. Write a Python program to calculate the length of a string. Go to the editor
Click me to see the sample solution
2. Write a Python program to count the number of characters (character frequency) in a string.
Go to the editor
Sample String : google.com’
Expected Result : {‘o’: 3, ‘g’: 2, ‘.’: 1, ‘e’: 1, ‘l’: 1, ‘m’: 1, ‘c’: 1}
Click me to see the sample solution
3. Write a Python program to get a string made of the first 2 and the last 2 chars from a given a
string. If the string length is less than 2, return instead of the empty string. Go to the editor
Sample String : ‘w3resource’
Expected Result : ‘w3ce’
Sample String : ‘w3’
Expected Result : ‘w3w3’
Sample String : ‘ w’
Expected Result : Empty String
Click me to see the sample solution
4. Write a Python program to get a string from a given string where all occurrences of its first
char have been changed to ‘$’, except the first char itself. Go to the editor
Sample String : ‘restart’
Expected Result : ‘resta$t’
Click me to see the sample solution
5. Write a Python program to get a single string from two given strings, separated by a space and
swap the first two characters of each string. Go to the editor
Sample String : ‘abc’, ‘xyz’
Expected Result : ‘xyc abz’
Click me to see the sample solution
6. Write a Python program to add ‘ing’ at the end of a given string (length should be at least 3). If
the given string already ends with ‘ing’ then add ‘ly’ instead. If the string length of the given
string is less than 3, leave it unchanged. Go to the editor
Sample String : ‘abc’
Expected Result : ‘abcing’
Sample String : ‘string’
Expected Result : ‘stringly’
Click me to see the sample solution
7. Write a Python program to find the first appearance of the substring ‘not’ and ‘poor’ from a
given string, if ‘not’ follows the ‘poor’, replace the whole ‘not’…’poor’ substring with ‘good’.
Return the resulting string. Go to the editor
Sample String : ‘The lyrics is not that poor!’
‘The lyrics is poor!’
Expected Result : ‘The lyrics is good!’
‘The lyrics is poor!’
Click me to see the sample solution
8. Write a Python function that takes a list of words and returns the length of the longest one. Go
to the editor
Click me to see the sample solution
9. Write a Python program to remove the nth index character from a nonempty string. Go to the
editor
Click me to see the sample solution
10. Write a Python program to change a given string to a new string where the first and last chars
have been exchanged. Go to the editor
Click me to see the sample solution
11. Write a Python program to remove the characters which have odd index values of a given
string. Go to the editor
Click me to see the sample solution
12. Write a Python program to count the occurrences of each word in a given sentence. Go to the
editor
Click me to see the sample solution
13. Write a Python script that takes input from the user and displays that input back in upper and
lower cases. Go to the editor
Click me to see the sample solution
14. Write a Python program that accepts a comma separated sequence of words as input and
prints the unique words in sorted form (alphanumerically). Go to the editor
Sample Words : red, white, black, red, green, black
Expected Result : black, green, red, white,red
Click me to see the sample solution
15. Write a Python function to create the HTML string with tags around the word(s). Go to the
editor
Sample function and result :
add_tags(‘i’, ‘Python’) -> ‘<i>Python</i>’
add_tags(‘b’, ‘Python Tutorial’) -> ‘<b>Python Tutorial </b>’
Click me to see the sample solution
16. Write a Python function to insert a string in the middle of a string. Go to the editor
Sample function and result :
insert_sting_middle(‘[[]]<<>>’, ‘Python’) -> [[Python]]
insert_sting_middle(‘{{}}’, ‘PHP’) -> {{PHP}}
Click me to see the sample solution
17. Write a Python function to get a string made of 4 copies of the last two characters of a
specified string (length must be at least 2). Go to the editor
Sample function and result :
insert_end(‘Python’) -> onononon
insert_end(‘Exercises’) -> eseseses
Click me to see the sample solution
18. Write a Python function to get a string made of its first three characters of a specified string.
If the length of the string is less than 3 then return the original string. Go to the editor
Sample function and result :
first_three(‘ipy’) –> ipy
first_three(‘python’) -> pyt
Click me to see the sample solution
19. Write a Python program to get the last part of a string before a specified character. Go to the
editor
https://www.w3resource.com/python-exercises
https://www.w3resource.com/python
Click me to see the sample solution
20. Write a Python function to reverses a string if it’s length is a multiple of 4. Go to the editor
Click me to see the sample solution
21. Write a Python function to convert a given string to all uppercase if it contains at least 2
uppercase characters in the first 4 characters. Go to the editor
Click me to see the sample solution
22.Write a Python program to sort a string lexicographically. Go to the editor
Click me to see the sample solution
23. Write a Python program to remove a newline in Python. Go to the editor
Click me to see the sample solution
24. Write a Python program to check whether a string starts with specified characters. Go to the
editor
Click me to see the sample solution
25. Write a Python program to create a Caesar encryption. Go to the editor
Note : In cryptography, a Caesar cipher, also known as Caesar’s cipher, the shift cipher, Caesar’s
code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a
type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed
number of positions down the alphabet. For example, with a left shift of 3, D would be replaced
by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his
private correspondence.
Click me to see the sample solution
26. Write a Python program to display formatted text (width=50) as output. Go to the editor
Click me to see the sample solution
27. Write a Python program to remove existing indentation from all of the lines in a given text.
Go to the editor
Click me to see the sample solution
28. Write a Python program to add a prefix text to all of the lines in a string. Go to the editor
Click me to see the sample solution
29. Write a Python program to set the indentation of the first line. Go to the editor
Click me to see the sample solution
30. Write a Python program to print the following floating numbers upto 2 decimal places. Go to
the editor
Click me to see the sample solution
31. Write a Python program to print the following floating numbers upto 2 decimal places with a
sign. Go to the editor
Click me to see the sample solution
32. Write a Python program to print the following floating numbers with no decimal places. Go
to the editor
Click me to see the sample solution
33. Write a Python program to print the following integers with zeros on the left of specified
width. Go to the editor
Click me to see the sample solution
34. Write a Python program to print the following integers with ‘*’ on the right of specified
width. Go to the editor
Click me to see the sample solution
35. Write a Python program to display a number with a comma separator. Go to the editor
Click me to see the sample solution
36. Write a Python program to format a number with a percentage. Go to the editor
Click me to see the sample solution
37. Write a Python program to display a number in left, right and center aligned of width 10. Go
to the editor
Click me to see the sample solution
38. Write a Python program to count occurrences of a substring in a string. Go to the editor
Click me to see the sample solution
39. Write a Python program to reverse a string. Go to the editor
Click me to see the sample solution
40. Write a Python program to reverse words in a string. Go to the editor
Click me to see the sample solution
41. Write a Python program to strip a set of characters from a string. Go to the editor
Click me to see the sample solution
42. Write a Python program to count repeated characters in a string. Go to the editor
Sample string: ‘thequickbrownfoxjumpsoverthelazydog’
Expected output :
o 4
e 3
u 2
h 2
r 2
t 2
Click me to see the sample solution
43. Write a Python program to print the square and cube symbol in the area of a rectangle and
volume of a cylinder. Go to the editor
Sample output:
The area of the rectangle is 1256.66cm2
The volume of the cylinder is 1254.725cm3
Click me to see the sample solution
44. Write a Python program to print the index of the character in a string. Go to the editor
Sample string: w3resource
Expected output:
Current character w position at 0
Current character 3 position at 1
Current character r position at 2
– – – – – – – – – – – – – – – – – – – – – – – – –
Current character c position at 8
Current character e position at 9
Click me to see the sample solution
45. Write a Python program to check if a string contains all letters of the alphabet. Go to the
editor
Click me to see the sample solution
46. Write a Python program to convert a string in a list. Go to the editor
Click me to see the sample solution
47. Write a Python program to lowercase first n characters in a string. Go to the editor
Click me to see the sample solution
48. Write a Python program to swap comma and dot in a string. Go to the editor
Sample string: “32.054,23”
Expected Output: “32,054.23”
Click me to see the sample solution
49. Write a Python program to count and display the vowels of a given text. Go to the editor
Click me to see the sample solution
50. Write a Python program to split a string on the last occurrence of the delimiter. Go to the
editor
Click me to see the sample solution
51. Write a Python program to find the first non-repeating character in given string. Go to the
editor
Click me to see the sample solution
52. Write a Python program to print all permutations with given repetition number of characters
of a given string. Go to the editor
Click me to see the sample solution
53. Write a Python program to find the first repeated character in a given string. Go to the editor
Click me to see the sample solution
54. Write a Python program to find the first repeated character of a given string where the index
of first occurrence is smallest. Go to the editor
Click me to see the sample solution
55.Write a Python program to find the first repeated word in a given string. Go to the editor
Click me to see the sample solution
56. Write a Python program to find the second most repeated word in a given string. Go to the
editor
Click me to see the sample solution
57.Write a Python program to remove spaces from a given string. Go to the editor
Click me to see the sample solution
58. Write a Python program to move spaces to the front of a given string. Go to the editor
Click me to see the sample solution
59. Write a Python program to find the maximum occurring character in a given string. Go to the
editor
Click me to see the sample solution
60. Write a Python program to capitalize first and last letters of each word of a given string. Go
to the editor
Click me to see the sample solution
61. Write a Python program to remove duplicate characters of a given string. Go to the editor
Click me to see the sample solution
62. Write a Python program to compute sum of digits of a given string. Go to the editor
Click me to see the sample solution
63. Write a Python program to remove leading zeros from an IP address. Go to the editor
Click me to see the sample solution
LIST
Python List [71 exercises with solution]