Вы находитесь на странице: 1из 3

9.

Implementing python programs using Strings


1. For each of the following expressions, what value will the expression give? Verify your
answers by printing the expressions.

a) ’Comp’ ’Sci’
b) ’Computer’ + ’ Science’
c) ’H20’ * 3
d) ’C02’ * 0

2. Assume that the variable data refers to the string “No way!” And find the following,

a) len(data)
b) data[1]
c) data[-1]
d) data[3:6]
e) data.replace(“No”, “Yes”)
f) data.find(“way!”)

3. Use the value of data from previous problem; write the values of the following expressions:
a) data.endswith('i')
b) “ totally “.join(data.split())

4. Given variables x and y, which refer to values 3 and 12.5 respectively, use print to display the
following messages. When numbers appear in the messages, the variables x and y should be
used in the print statement.
a) The rabbit is 3.
b) The rabbit is 3 years old.
c) 12.5 is average.
d) 12.5 * 3
e) 12.5 * 3 is 37.5.

5. State whether each expression listed here evaluates to True or False:


a) ’g’ == "g"
b) ’g’ == ’G’
c) ’a’ ≥ ’b’
d) ’ant’ < ’abc’
e) ’ant’ > ’Ant’
f) ’ant’ > ’Abc’
g) ’ant’ < ’anti’
h) 'p' in 'Pinapple'
i) 'apple' in 'Pinapple'
6. Write a Python program to convert a list to a string.

7. Assume that the variable data refers to the string “Python rules!” Use a string method to
perform the following tasks:
a) Obtain a list of the words in the string.
b) Convert the string to uppercase.
c) Locate the position of the string “rules”.
d) Replace the exclamation point with a question mark.

8. For variable product_code containing a string of letters and digits,


a) Give an’ if statement’ that outputs “Verified” if product_code contains both a “Z” and a
“9”, and outputs “Failed” otherwise.
b) Give a Python instruction that prints out just the last three characters in product_code.

9. Give an instruction that determines the index of the ‘@’ character in an email address in
variable email_addr.

10. For variable date containing a date of the form 12/14/2012, write a function that produces the
same date, but with all slashes characters replaced with dashes.

11. For a variable named err_mesg that contains error messages in the form:

** error message **, give an instruction that produces a string containing the error message
without the leading and trailing asterisks and blank characters.

12. Take the following Python code that stores a string: str = 'X-DSPAM-Confidence: 0.8475'
Use find and string slicing to extract the portion of the string after the colon character and then
use the float function to convert the extracted string into a floating point number.

13. Write a program to read a string from the user. Give a ‘for’ loop that counts all the characters
in the given string.

14. Write a program to find the number of vowels, consonants, digits, white space characters and
special characters in a given string.

15. Write a program to check if a given string is a Palindrome. A palindrome reads same from
front and back e.g.- aba, ccaacc, mom, etc

16. Write a program to read ‘n’ friends name and store it in a list and then sort those names in
alphabetically ascending order.
17. Write a program to check if the two strings entered by user are anagrams or not. Two words
are said to be anagrams if the letters of one word can be rearranged to form the other word. For
example, jaxa and ajax are anagrams of each other.

18. Write a program that takes your full name as input and displays the abbreviations of the first
and middle names except the last name which is displayed as it is. For example, if your name is
Chandrasekhara Venkata Raman, then the output should be C. V. Raman.

19. 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 empty string.
Sample String: 'welcome'
Expected Result: 'weme'
Sample String: 'x4'
Expected Result: 'x4x4'
Sample String: ' w'
Expected Result: Empty String

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

21. Write a program to make a new string with the word "the" deleted in the sentence "This is the
flower in the garden".

22. Write a program to make a new string with all the consonants deleted from the string "Hello,
have a good day".

23. Write a program to find out the largest and smallest word in the string "This is an umbrella".

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

25. Write a Python program to create a Caesar encryption.


Note: In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's
code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a
type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed
number of positions down the alphabet. For example, with a left shift of 3, D would be replaced
by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his
private correspondence.

Вам также может понравиться