# HG changeset patch # User Puneeth Chaganti # Date 1286698569 -19800 # Node ID 6548b2248b512abf79a3ecb015add14ce9efb0f0 # Parent 31fc2f22ff307df46df88daae549c0d922b15984# Parent a4203cc8d4a21649e36fad6f013de3c78a9ac17b Merged heads, again. diff -r a4203cc8d4a2 -r 6548b2248b51 manipulating-strings/questions.rst --- a/manipulating-strings/questions.rst Sun Oct 10 13:44:19 2010 +0530 +++ b/manipulating-strings/questions.rst Sun Oct 10 13:46:09 2010 +0530 @@ -1,17 +1,94 @@ -Objective ---------- +Objective Questions +------------------- + +.. A mininum of 8 questions here (along with answers) + +1. Given the list week:: + + ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] + + ``"Sun" in week`` returns True or False? + + Answer: False + +#. Given the string ``s = "palindrome"``, what is returned by s[4:] + + Answer: ``ndrome`` + +#. Given the string ``s = "palindrome"``, what is returned by s[-4] + + Answer: ``r`` + +#. Given the string ``s = "palindrome"``, what is returned by s[4::-1] -.. A mininum of 8 questions here. + Answer: ``nilap`` + +#. Given the string ``s = "palindrome"``, what is returned by s[-4:] + + Answer: ``rome`` + +#. Given a string ``s = "this is a string"``, how will you change it + to ``"this isn't a list"`` ? -1. Question 1 -2. Question 2 -3. Question 3 + Answer:: + + s = s.replace("string", "list") + s = s.replace("is", "isn't") + +#. Given a string ``s = "this is a string"``, how will you change it + to ``"THIS ISN'T A LIST"`` ? + + Answer:: + + s = s.replace("string", "list") + s = s.replace("is", "isn't") + s = s.upper() +#. Given a line from a CSV file (comma separated values), convert it + to a space separated line. -Programming ------------ + Answer: line.replace(',' ' ') + +#. Given the string "F.R.I.E.N.D.S" in s, obtain the friends. + + Answer: ``s[::2].lower() + +Larger Questions +---------------- + +.. A minimum of 2 questions here (along with answers) + +1. Given the string friends, obtain the string "F.R.I.E.N.D.S". + + Answer:: + + s = "friends" + s = s.upper() + s_list = list(s) + ".".join(s_list) -.. A minimum of 2 questions here. +2. Given a string with double quotes and single quotes. Interchange + all the double quotes to single quotes and vice-versa. [Solve it + for this particular string. Not a generic one. Solve it for a + generic string, if you know how to iterate over a list.] + + s = """ "Isn't this a 'simple' task?" "No, it isn't." "Yes! it is." """ + + Answer:: + + s = s.replace('"', '#') + s = s.replace("'", '"') + s = s.replace('#', "'") -1. Programming Assignment 1 -2. Programming Assignment 2 + .. depends on ``for`` which is not an LO dependency a generic string. + + For generic string + Answer:: + + S = [] + s_list = s.split("'") + + for s_s in s_list: + S.append(s_s.replace('"', "'")) + + s = '"'.join(S) diff -r a4203cc8d4a2 -r 6548b2248b51 manipulating-strings/script.rst --- a/manipulating-strings/script.rst Sun Oct 10 13:44:19 2010 +0530 +++ b/manipulating-strings/script.rst Sun Oct 10 13:46:09 2010 +0530 @@ -1,18 +1,40 @@ -======== - Script -======== +.. Objectives +.. ---------- + +.. By the end of this tutorial, you will be able to + +.. 1. Slice strings and get sub-strings out of them +.. #. Reverse strings +.. #. Replace characters in strings. +.. #. Convert to strings to upper or lower case +.. #. joining a list of strings + +.. Prerequisites +.. ------------- -{{{ show the welcome slide }}} +.. 1. getting started with strings +.. #. getting started with lists +.. #. basic datatypes + +.. Author : Puneeth + Internal Reviewer : + External Reviewer : + Checklist OK? : [2010-10-05] -Welcome to this tutorial on manipulating strings. +Script +------ + +{{{ Show the slide containing title }}} + +Hello Friends. Welcome to this tutorial on manipulating strings. {{{ show the slide with outline }}} In this tutorial we shall learn to manipulate strings, specifically slicing and reversing them, or replacing characters, converting from -upper to lower case and vice-versa +upper to lower case and vice-versa and joining a list of strings. -#[punch: reversed returns an iterator. should we still teach it?] +.. #[punch: reversed returns an iterator. should we still teach it?] We have an ``ipython`` shell open, in which we are going to work, through out this session. @@ -59,8 +81,12 @@ As we already know, the last element of the string can be accessed using ``s[-1]``. -%%1%% Pause the video here and obtain the sub-string excluding the -first and last characters from the string. +Following is an exercise that you must do. + +%%1%% Obtain the sub-string excluding the first and last characters +from the string. + +Please, pause the video here. Do the exercise(s) and then continue. :: @@ -131,10 +157,13 @@ Note that these methods, do not change the original string, but return a new string. -a%% %% Pause the video here, and finish the problem of checking if -``s`` is a valid name of a day of the week and then resume the -video. Change the solution to this problem, to include forms like, -SAT, SATURDAY, Saturday and Sat. +Following is an exercise that you must do. + +%%2%% Check if ``s`` is a valid name of a day of the week. Change the +solution to this problem, to include forms like, SAT, SATURDAY, +Saturday and Sat. + +Please, pause the video here. Do the exercise and then continue. :: @@ -160,20 +189,59 @@ email = email.replace("[at]", "@") print email -%%1%% Pause the video here and replace the ``[dot]`` with ``.`` and then -resume the video. +Following is an exercise that you must do. + +%%3%% Replace the ``[dot]`` with ``.`` + +Please, pause the video here. Do the exercise and then continue. :: email = email.replace("[dot]", ".") print email +Now, let's look at another interesting problem where we have a list of +e-mail addresses and we wish to obtain one long string of e-mail +addresses separated by commas or semi-colons. + +:: + + email_list = ["info@fossee.in", "enquiries@fossee.in", "help@fossee.in"] + + +Now, if we wish to obtain one long string, separating each of the +email id by a comma, we use the join operator on ``,``. + +:: + + email_str = ", ".join(email_list) + print email_str + +Notice that the email ids are joined by a comma followed by a space. + +Following is an exercise that you must do. + +%%3%% From the email_str that we generated, change the separator to be +a semicolon instead of a comma. + +Please, pause the video here. Do the exercise and then continue. + +:: + + email_str = email_str.replace(",", ";") That brings us to the end of the tutorial. {{{ show summary slide }}} In this tutorial, we have learnt how to get substrings, reverse -strings and a few useful methods, namely upper, lower and replace. +strings and a few useful methods, namely upper, lower, replace and +join. + +{{{ Show the "sponsored by FOSSEE" slide }}} -Thank You! +This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India + +Hope you have enjoyed and found it useful. +Thank you! +