Python Strings Programs

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

1.

 Write a Python program to calculate the length of a string. 

2. Write a Python program to count the number of characters (character


frequency) in a string. 
Sample String : google.com'
Expected Result :

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

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 program to reverse a string. 

16. Write a Python program to reverse words in a string. 

17. Write a Python program to check if a string contains all letters of the


alphabet  

18. Write a Python program to convert a string in a list. 

19. Write a Python program to capitalize first and last letters of each word
of a given string. 

20. Write a Python program to remove leading zeros from an IP address. 

21. Write a Python program to count Uppercase, Lowercase, special


character and numeric values in a given string. 
22. Write a Python program to find smallest and largest word in 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

24. Write a Python program to remove duplicate words from a given string. 


Sample Output:
Original String:
Python Exercises Practice Solution Exercises
After removing duplicate words from the said string:
Python Exercises Practice Solution

25. WAP to enter two strings and check whether they are Palindrome
or not.

You might also like