Assignment1 (Strings)
Assignment1 (Strings)
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.
Sample String : 'w3resource'
Expected Result : 'w3ce'
Sample String : 'w3'
Expected Result : 'w3w3'
Sample String : ' w'
Expected Result : Empty String
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.
Sample String : 'restart'
Expected Result : 'resta$t'
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.
Sample String : 'abc', 'xyz'
Expected Result : 'xyc abz'
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.
Sample String : 'abc'
Expected Result : 'abcing'
Sample String : 'string'
Expected Result : 'stringly'
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.
Sample String : 'The lyrics is not that poor!'
'The lyrics is poor!'
Expected Result : 'The lyrics is good!'
'The lyrics is poor!'
8. Write a Python function that takes a list of words and returns the length of the
longest one.
9. Write a Python program to remove the nth index character from a nonempty
string.
10. Write a Python program to change a given string to a new string where the
first and last chars have been exchanged.
11. Write a Python program to remove the characters which have odd index
values of a given string.
12. Write a Python program to count the occurrences of each word in a given
sentence.
13. Write a Python script that takes input from the user and displays that input
back in upper and lower cases.
14. Write a Python program that accepts a comma separated sequence of words
as input and prints the unique words in sorted form (alphanumerically).
Sample Words : red, white, black, red, green, black
Expected Result : black, green, red, white,red
15. Write a Python function to create the HTML string with tags around the
word(s).
Sample function and result :
add_tags('i', 'Python') -> '<i>Python</i>'
add_tags('b', 'Python Tutorial') -> '<b>Python Tutorial </b>'
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).
Sample function and result :
insert_end('Python') -> onononon
insert_end('Exercises') -> eseseses
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.
Sample function and result :
first_three('ipy') -> ipy
first_three('python') -> pyt
19. Write a Python program to get the last part of a string before a specified
character.
https://www.w3resource.com/python-exercises
https://www.w3resource.com/python
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.
24. Write a Python program to check whether a string starts with specified
characters.
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.
27. Write a Python program to remove existing indentation from all of the lines in
a given text.
28. Write a Python program to add a prefix text to all of the lines in a string.
29. Write a Python program to set the indentation of the first line.
30. Write a Python program to print the following floating numbers upto 2 decimal
places.
31. Write a Python program to print the following floating numbers upto 2 decimal
places with a sign.
32. Write a Python program to print the following floating numbers with no
decimal places.
33. Write a Python program to print the following integers with zeros on the left of
specified width.
34. Write a Python program to print the following integers with '*' on the right of
specified width.
37. Write a Python program to display a number in left, right and center aligned
of width 10.
43. Write a Python program to print the square and cube symbol in the area of a
rectangle and volume of a cylinder.
Sample output:
The area of the rectangle is 1256.66cm2
The volume of the cylinder is 1254.725cm3
44. Write a Python program to print the index of the character in a string.
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
45. Write a Python program to check if a string contains all letters of the
alphabet.
49. Write a Python program to count and display the vowels of a given text.
50. Write a Python program to split a string on the last occurrence of the
delimiter.
51. Write a Python program to find the first non-repeating character in given
string.
52. Write a Python program to print all permutations with given repetition number
of characters of a given string.
53. Write a Python program to find the first repeated character in a given string.
54. Write a Python program to find the first repeated character of a given string
where the index of first occurrence is smallest.
55.Write a Python program to find the first repeated word in a given string.
56. Write a Python program to find the second most repeated word in a given
string.
58. Write a Python program to move spaces to the front of a given string.
59. Write a Python program to find the maximum occurring character in a given
string.
60. Write a Python program to capitalize first and last letters of each word of a
given string.