0% found this document useful (0 votes)
4 views

Python Strings

The document provides a series of Python programming exercises focused on string manipulation. Tasks include calculating string length, character frequency, substring replacement, and word counting, among others. Each exercise includes sample strings and expected results to guide implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Strings

The document provides a series of Python programming exercises focused on string manipulation. Tasks include calculating string length, character frequency, substring replacement, and word counting, among others. Each exercise includes sample strings and expected results to guide implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Strings

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': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}

3. Write a Python program to get a string made of the first 2 and last 2 characters of a given string. If

9
the string length is less than 2, return the empty string instead.

64
Sample String : 'Encoderrs'
Expected Result : 'Enrs'
Sample String : 'En'
Expected Result : 'EnEn'

17
Sample String : 'E'
Expected Result : Empty String

77
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' 61
Expected Result : 'resta$t'

5. Write a Python program to get a single string from two given strings, separated by a space and swap
/8
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
du

given string already ends with 'ing', add 'ly' instead. If the string length of the given string is less than
3, leave it unchanged.
Sample String : 'abc'
en

Expected Result : 'abcing'


Sample String : 'string'
Expected Result : 'stringly'
ry

7. Write a Python program to find the first appearance of the substrings 'not' and 'poor' in a given
string. If 'not' follows 'poor', replace the whole 'not'...'poor' substring with 'good'. Return the resulting
string.
Su

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 return 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 newly string where the first and last chars
have been exchanged.

11. Write a Python program to remove characters that have odd index values in a given string.

12. Write a Python program to count the occurrences of each word in a given sentence.

9
13. Write a Python script that takes input from the user and displays that input back in upper and

64
lower cases.

14. Write a Python program that accepts a comma-separated sequence of words as input and prints the
distinct words in sorted form (alphanumerically).

17
Sample Words : red, white, black, red, green, black
Expected Result : black, green, red, white,red
Click me to see the sample solution

77
15. Write a Python function to create an HTML string with tags around the word(s).
Sample function and result :
61
add_tags('i', 'Python') -> '<i>Python</i>'
add_tags('b', 'Python Tutorial') -> '<b>Python Tutorial </b>'
/8
du
en
ry
Su

You might also like