0% found this document useful (0 votes)
3 views2 pages

06 String Assignment 02 - Programs

This is it.

Uploaded by

rupshadas1403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

06 String Assignment 02 - Programs

This is it.

Uploaded by

rupshadas1403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

String

Type A: Short Answer Questions/Conceptual Questions


Q.1 Write a Python script that traverses through an input string and prints its characters in different lines — two
characters per line.
Q.2 Out of the given operators, which ones can be used with strings in Python: =, -, *, /, //, %, >, <>, in, not in, <=
Q.3 What is the result of following statement, if the input is 'Fun'? print(input("Enter Value: ") + "trial" + "Ooty" * 3)
Q.4 Which of the following is not a Python legal string operation?
(a) 'abc' + 'abc' (b) 'abc' * 3 (c) 'abc' + .3 (d) 'abc.lower()
Q.5 Can you say strings are character lists? Why? Why not?
Q.6 Given a string S = "CARPE DIEM". If n is length/2 (length is the length of the given string), then what would
following return?
(a) S[: n] (b) S[n :] (c) S[n : n] (d) S[1 : n] (e) S[n : length - 1]
Q.7 From the string S = "CARPE DIEM", which ranges return "DIE" and "CAR"?
Q.8 What happens when from a string slice you skip the start and/or end values of the slice?
Q.9 What would the following expressions return?
a. "Hello World".upper( ).lower( ) b. "Hello World".lower( ).upper( ) c. "Hello World".find("Wor",1,6)
d. "Hello World".find("Wor") e. "Hello World".find("wor") f. "Hello World".isalpha( )
g. "Hello World".isalnum( ) h. "1234".isdigit( ) i. "123FGH".isdigit( )
Q.10 Which functions would you choose to use to remove leading and trailing white spaces from a given string?
Q.11 Try to find out if for any case, the string functions isalnum( ) and isalpha( ) return the same result
Q.12 Suggest appropriate functions for the following tasks:
1. To check whether the string contains digits
2. To find for the occurrence a string within another string
3. To convert the first letter of a string to upper case
4. to capitalize all the letters of the string
5. to check whether all letters of the string are in capital letters
6. to remove from right of a string all string-combinations from a given set of letters
7. to remove all white spaces from the beginning of a string
Q.13 In a string slice, the start and end values can be beyond limits. Why?
Q.14 Can you specify an out of bound index when accessing a single character from a string? Why?
Q.15 Can you add two strings? What effect does ' + ' have on strings?
Type C: Programming Practice/Knowledge based Questions
Q.1 Write a program to count the number of times a character occurs in the given string.
Q.2 Write a program which replaces all vowels in the string with '*'.
Q.3 Write a program which reverses a string and stores the reversed string in a new string.
Q.4 Write a program that prompts for a phone number of 10 digits and two dashes, with dashes after the area code and
the next three numbers. For example, 017-555-1212 is a legal input. Display if the phone number entered is valid
format or not and display if the phone number is valid or not (i.e., contains just the digits and dash at specific
places.)
Q.5 Write a program that should do the following :
• prompt the user for a string
• extract all the digits from the string
• If there are digits:
o sum the collected digits together
o print out the original string, the digits, the sum of the digits
• If there are no digits:
o print the original string and a message "has no digits"
Sample
• given the input : abc123
prints abc123 has the digits 123 which sum to 6
• given the input : abcd
prints abcd has no digits
Q6 Write a program that should prompt the user to type some sentence(s) followed by "enter". It should then print the
original sentence(s) and the following statistics relating to the sentence(s) :
• Number of words
• Number of characters (including white-space and punctuation)
• Percentage of characters that are alphanumeric
Hints: Assume any consecutive sequence of non-blank characters is a word.
Q.7 Write a Python program as per specifications given below:
• Repeatedly prompt for a sentence (string) or for 'q' to quit.
• Upon input of a sentence s, print the string produced from s by converting each lower case letter to upper case
and each upper case letter to lower case.
• All other characters are left unchanged.
For example,
Please enter a sentence, or 'q' to quit : This is the Bomb!
tHIS IS THE bOMB!
Please enter a sentence, or 'q ' to quit : What's up Doc ???
wHAT'S UP dOC ???
Please enter a sentence, or 'q' to quit : q
Q.8 Write a program that does the following :
• takes two inputs : the first, an integer and the second, a string
• from the input string extract all the digits, in the order they occurred, from the string.
o if no digits occur, set the extracted digits to 0
• add the integer input and the digits extracted from the string together as integers
• print a string of the form :
"integer_input + string_digits = sum"
For example :
For inputs 12, 'abc123' → '12 + 123 = 135'
For inputs 20, 'a5b6c7' → '20 + 567 =587'
For inputs 100, 'hi mom' → '100 + 0 = 100'
Q.9 Write a program that takes two strings from the user and displays the smaller string in single line and the larger
string as per this format :
1st letter last letter
2nd letter 2nd last letter
3rd letter 3rd last letter
For example, if the two strings entered are Python and PANDA then the output of the program should be
PANDA
P n
y o
t h
Q.10 Write a program to convert a given number into equivalent Roman number (store its value as a string). You can
use following guidelines to develop solution for it:
• From the given number, pick successive digits, using %10 and /10 to gather the digits from right to left.
• The rules for Roman Numerals involve using four pairs of symbols for ones and five, tens and fifties, hundreds
and five hundreds. An additional symbol for thousands covers all the relevant bases.
• When a number is followed by the same or smaller number, it means addition. "II" is two 1's = 2. "VI" is 5 + 1 =
6.
• When one number is followed by a larger number, it means subtraction. "IX" is 1 before 10 = 9. "IIX isn't
allowed, this would be "VIII". For numbers from 1 to 9, the symbols are "I" and "V", and the coding works like
this. "I" , "II", "III", "IV", "V", "VI", "VII", "VIII", "IX".
• The same rules work for numbers from 10 to 90, using "X" and "L". For numbers from 100 to 900, using the
symbols "C" and "D". For numbers between 1000 and 4000, using "M".
Here are some examples. 1994 = MCMXCIV, 1956 = MCMLVI, 3888= MMMDCCCLXXXVIII
Q.11 Write a program that asks the user for a string (only single space between words) and returns an estimate of how
many words are in the string. (Hint. Count number of spaces)
Q.12 Write a program to input a formula with some brackets and checks, and prints out if the formula has the same
number of opening and closing parentheses.
Q.13 Write a program that inputs a line of text and prints out the count of vowels in it.
Q.14 Write a program to input a line of text and print the biggest word (length wise) from it.
Q.15 Write a program to input a line of text and create a new line of text where each word of input line is reversed.

You might also like