Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

String_Program_Worksheet

Uploaded by

shekharv8102
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

String_Program_Worksheet

Uploaded by

shekharv8102
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Revision Test 2024-25

Subject: CS Grade: XI
Marks: Time:
Name: ___________ Date:
__________________________________________________________________

1. Write a Python program to get a string made of the first 2 and last 2 characters of a given string.
If the string length is less than 2, return the empty string instead.

Sample String : 'w3resource'


Expected Result : 'w3ce'
Sample String : 'w3'
Expected Result : 'w3w3'
Sample String : ' w'
Expected Result : Empty String
2. 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'

3. 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'

4. 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', 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'

5. 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.
Sample String : 'The lyrics is not that poor!'

'The lyrics is poor!'

Expected Result : 'The lyrics is good!'

'The lyrics is poor!'

6. 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

7. Write a Python program to change a given string to a newly string where the first and last chars
have been exchanged.
8. Write a Python program to remove characters that have odd index values in each string.
9. 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

10. Write a Python program to count repeated characters in a string.

Sample string: 'thequickbrownfoxjumpsoverthelazydog'

Expected output :

o4

e3

u2

h2

11. Write a Python program to remove all characters except a specified character from a given
string.

Original string

Python Exercises

Remove all characters except P in the said string:

Original string
google

Remove all characters except g in the said string:

gg

Original string

exercises

Remove all characters except e in the said string:

Eee

You might also like