Python Strings Programs
Python Strings Programs
Python Strings Programs
g is occurring 2 times
o is occurring 3 times
…
..
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 longest
word and the length of the longest one.
Sample Output:
Longest word: Exercises
Length of the longest word: 9
13. Write a Python script that takes input from the user and displays that
input back in upper and lower cases.
19. Write a Python program to capitalize first and last letters of each word
of a given string.
23. Write a Python program find the common values that appear in two
given strings.
Sample Output:
Original strings:
Python3
Python2.7
Intersection of two said String:
Python
25. WAP to enter two strings and check whether they are Palindrome
or not.