summaryrefslogtreecommitdiff
path: root/manipulating_strings
diff options
context:
space:
mode:
authorPuneeth Chaganti2010-12-01 16:51:35 +0530
committerPuneeth Chaganti2010-12-01 16:51:35 +0530
commitf3a34dfb4e879f3eb7274704f44546aac4add88f (patch)
tree1cb0a8cc5dbd5ee2b374350915ed2addfa0fb447 /manipulating_strings
parent347866ed0d29db61ee062563b1e1616cfb85588c (diff)
downloadst-scripts-f3a34dfb4e879f3eb7274704f44546aac4add88f.tar.gz
st-scripts-f3a34dfb4e879f3eb7274704f44546aac4add88f.tar.bz2
st-scripts-f3a34dfb4e879f3eb7274704f44546aac4add88f.zip
Renamed all LOs to match with their names in progress.org.
Diffstat (limited to 'manipulating_strings')
-rw-r--r--manipulating_strings/quickref.tex14
-rw-r--r--manipulating_strings/script.rst255
-rw-r--r--manipulating_strings/slides.org94
-rw-r--r--manipulating_strings/slides.tex150
4 files changed, 513 insertions, 0 deletions
diff --git a/manipulating_strings/quickref.tex b/manipulating_strings/quickref.tex
new file mode 100644
index 0000000..533a1ef
--- /dev/null
+++ b/manipulating_strings/quickref.tex
@@ -0,0 +1,14 @@
+\textbf{Manipulating strings}
+
+String indexing starts from 0, like lists.
+
+\lstinline|s = `Hello World'|\\
+\lstinline|s[0:5]| gives \texttt{Hello}\\
+\lstinline|s[6:]| gives \textt{World}\\
+\lstinline|s[6::2]| gives \textt{Wrd}\\
+
+\lstinline|s.replace('e', 'a')| returns a new string with all e's
+replaced by a.
+
+\lstinline|s.lower()| and \lstinline|s.upper()| return new strings
+with all lower and upper case letters, respectively.
diff --git a/manipulating_strings/script.rst b/manipulating_strings/script.rst
new file mode 100644
index 0000000..3cc1b9c
--- /dev/null
+++ b/manipulating_strings/script.rst
@@ -0,0 +1,255 @@
+.. 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 strings to upper or lower case
+.. #. joining a list of strings
+
+.. Prerequisites
+.. -------------
+
+.. 1. getting started with strings
+.. #. getting started with lists
+.. #. basic datatypes
+
+.. Author : Puneeth
+ Internal Reviewer : Amit
+ External Reviewer :
+ Language Reviewer : Bhanukiran
+ Checklist OK? : <08-11-2010, Anand, OK> [2010-10-05]
+
+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 and joining a list of strings.
+
+We have an ``ipython`` shell open, in which we are going to work,
+through out this session.
+
+Let us consider a simple problem, and learn how to slice strings and
+get sub-strings.
+
+Let's say the variable ``week`` has the list of the names of the days
+of the week.
+
+::
+
+ week = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
+
+
+Now given a string ``s``, we should be able to check if the string is a
+valid name of a day of the week or not.
+
+::
+
+ s = saturday
+
+
+``s`` could be in any of the forms --- sat, saturday, Sat, Saturday,
+SAT, SATURDAY. For now, shall now be solving the problem only for the forms,
+sat and saturday. We shall solve it for the other forms, at the end of
+the tutorial.
+
+{{{ show these forms in a slide }}}
+
+So, we need to check if the first three characters of the given string
+exists in the variable ``week``.
+
+As, with any of the sequence data-types, strings can be sliced into
+sub-strings. To get the first three characters of s, we say,
+
+::
+
+ s[0:3]
+
+Note that, we are slicing the string from the index 0 to index 3, 3
+not included.
+
+As we already know, the last element of the string can be accessed
+using ``s[-1]``.
+
+Following is an exercise that you must do.
+
+%%1%% Obtain the sub-string excluding the first and last characters
+from the string s.
+
+Please, pause the video here. Do the exercise(s) and then continue.
+
+::
+
+ s[1:-1]
+
+gives the substring of s, without the first and the last
+characters of s.
+
+::
+
+ s = saturday
+ s[:3]
+
+Now, we just check if that substring is present in the variable
+``week``.
+
+::
+
+ s[:3] in week
+
+Let us now consider the problem of finding out if a given string is
+palindromic or not. First of all, a palindromic string is a string
+that remains same even when it has been reversed.
+
+Let the string given be ``malayalam``.
+
+::
+
+ s = "malayalam"
+
+Now, we need to compare this string with it's reverse.
+
+Again, we will use a technique common to all sequence data-types,
+[::-1]
+
+So, we obtain the reverse of s, by simply saying,
+
+::
+
+ s[::-1]
+
+Now, to check if the string is ``s`` is palindromic, we say
+::
+
+ s == s[::-1]
+
+As, expected, we get ``True``.
+
+Now, if the string we are given is ``Malayalam`` instead of
+``malayalam``, the above comparison would return a False. So, we will
+have to convert the string to all lower case or all upper case, before
+comparing. Python provides methods, ``s.lower`` and ``s.upper`` to
+achieve this.
+
+Let's try it out.
+::
+
+ s = "Malayalam"
+
+ s.upper()
+
+ s
+
+As you can see, s has not changed. It is because, ``upper`` returns a
+new string. It doesn't change the original string.
+
+::
+
+ s.lower()
+
+ s.lower() == s.lower()[::-1]
+
+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.
+
+::
+
+ s in week
+
+ s.lower()[:3] in week
+
+
+So, as you can see, now we can check for presence of ``s`` in
+``week``, in whichever format it is present -- capitalized, or all
+caps, full name or short form.
+
+We just convert any input string to lower case and then check if it is
+present in the list ``week``.
+
+Now, let us consider another problem. We often encounter e-mail id's
+which have @ and periods replaced with text, something like
+info[at]fossee[dot]in. We now wish to get back proper e-mail
+addresses.
+
+Let's say the variable email has the email address.
+::
+
+ email = "info[at]fossee[dot]in"
+
+Now, we first replace the ``[at]`` with the ``@``, using the replace
+method of strings.
+::
+
+ email = email.replace("[at]", "@")
+ print email
+
+Following is an exercise that you must do.
+
+%%3%% Replace the ``[dot]`` with ``.`` in ``email``
+
+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, replace and
+join.
+
+{{{ Show the "sponsored by FOSSEE" slide }}}
+
+This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
+
+Hope you have enjoyed and found it useful.
+Thank you!
+
diff --git a/manipulating_strings/slides.org b/manipulating_strings/slides.org
new file mode 100644
index 0000000..cb8adfd
--- /dev/null
+++ b/manipulating_strings/slides.org
@@ -0,0 +1,94 @@
+#+LaTeX_CLASS: beamer
+#+LaTeX_CLASS_OPTIONS: [presentation]
+#+BEAMER_FRAME_LEVEL: 1
+
+#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
+#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra)
+#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC
+
+#+LaTeX_CLASS: beamer
+#+LaTeX_CLASS_OPTIONS: [presentation]
+
+#+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl}
+#+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet}
+
+#+LaTeX_HEADER: \usepackage{listings}
+
+#+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries,
+#+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
+#+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries}
+
+#+TITLE: Manipulating strings
+#+AUTHOR: FOSSEE
+#+EMAIL:
+#+DATE:
+
+#+DESCRIPTION:
+#+KEYWORDS:
+#+LANGUAGE: en
+#+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
+#+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc
+
+* Outline
+ - Slicing strings to get sub-strings
+ - Reversing strings
+ - Replacing characters in strings.
+ - Converting strings to upper or lower case
+ - Joining a list of strings
+* Question 1
+ Obtain the sub-string excluding the first and last characters from
+ the string ~s~.
+* Solution 1
+ #+begin_src python
+ In []: s[1:-1]
+ #+end_src
+* Question 2
+ Given a list week, week = ~week = ["sun", "mon", "tue", "wed",
+ "thu", "fri", "sat"]~. 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.
+* Solution 2
+ #+begin_src python
+ In []: s in week
+ In []: s.lower()[:3] in week
+ #+end_src
+* Question 3
+ Given ~email~ -- ~info@fossee[dot]in~
+
+ Replace the ~[dot]~ with ~.~ in ~email~
+* Solution 3
+ #+begin_src python
+ email.replace('[dot], '.')
+ print email
+ #+end_src
+* Question 4
+ From the ~email_str~ that we generated, change the separator to be a
+ semicolon instead of a comma.
+* Solution 4
+ #+begin_src python
+ email_str = email_str.replace(",", ";")
+ #+end_src
+* Summary
+ You should now be able to --
+ - Slice strings and get sub-strings out of them
+ - Reverse strings
+ - Replace characters in strings.
+ - Convert strings to upper or lower case
+ - Join a list of strings
+
+* Thank you!
+#+begin_latex
+ \begin{block}{}
+ \begin{center}
+ This spoken tutorial has been produced by the
+ \textcolor{blue}{FOSSEE} team, which is funded by the
+ \end{center}
+ \begin{center}
+ \textcolor{blue}{National Mission on Education through \\
+ Information \& Communication Technology \\
+ MHRD, Govt. of India}.
+ \end{center}
+ \end{block}
+#+end_latex
+
+
diff --git a/manipulating_strings/slides.tex b/manipulating_strings/slides.tex
new file mode 100644
index 0000000..ed0317b
--- /dev/null
+++ b/manipulating_strings/slides.tex
@@ -0,0 +1,150 @@
+% Created 2010-10-28 Thu 11:35
+\documentclass[presentation]{beamer}
+\usepackage[latin1]{inputenc}
+\usepackage[T1]{fontenc}
+\usepackage{fixltx2e}
+\usepackage{graphicx}
+\usepackage{longtable}
+\usepackage{float}
+\usepackage{wrapfig}
+\usepackage{soul}
+\usepackage{t1enc}
+\usepackage{textcomp}
+\usepackage{marvosym}
+\usepackage{wasysym}
+\usepackage{latexsym}
+\usepackage{amssymb}
+\usepackage{hyperref}
+\tolerance=1000
+\usepackage[english]{babel} \usepackage{ae,aecompl}
+\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet}
+\usepackage{listings}
+\lstset{language=Python, basicstyle=\ttfamily\bfseries,
+commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
+showstringspaces=false, keywordstyle=\color{blue}\bfseries}
+\providecommand{\alert}[1]{\textbf{#1}}
+
+\title{Manipulating strings}
+\author{FOSSEE}
+\date{}
+
+\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
+\begin{document}
+
+\maketitle
+
+
+
+
+
+
+
+
+
+\begin{frame}
+\frametitle{Outline}
+\label{sec-1}
+
+\begin{itemize}
+\item Slicing strings to get sub-strings
+\item Reversing strings
+\item Replacing characters in strings.
+\item Converting strings to upper or lower case
+\item Joining a list of strings
+\end{itemize}
+\end{frame}
+\begin{frame}
+\frametitle{Question 1}
+\label{sec-2}
+
+ Obtain the sub-string excluding the first and last characters from
+ the string \texttt{s}.
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solution 1}
+\label{sec-3}
+
+\begin{verbatim}
+In []: s[1:-1]
+\end{verbatim}
+\end{frame}
+\begin{frame}
+\frametitle{Question 2}
+\label{sec-4}
+
+ Given a list week, week = \texttt{week = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]}. Check if \texttt{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.
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solution 2}
+\label{sec-5}
+
+\begin{verbatim}
+In []: s in week
+In []: s.lower()[:3] in week
+\end{verbatim}
+\end{frame}
+\begin{frame}
+\frametitle{Question 3}
+\label{sec-6}
+
+ Given \texttt{email} -- \texttt{info@fossee[dot]in}
+
+ Replace the \texttt{[dot]} with \texttt{.} in \texttt{email}
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solution 3}
+\label{sec-7}
+
+\begin{verbatim}
+email.replace('[dot], '.')
+print email
+\end{verbatim}
+\end{frame}
+\begin{frame}
+\frametitle{Question 4}
+\label{sec-8}
+
+ From the \texttt{email\_str} that we generated, change the separator to be a
+ semicolon instead of a comma.
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solution 4}
+\label{sec-9}
+
+\begin{verbatim}
+email_str = email_str.replace(",", ";")
+\end{verbatim}
+\end{frame}
+\begin{frame}
+\frametitle{Summary}
+\label{sec-10}
+
+ You should now be able to --
+\begin{itemize}
+\item Slice strings and get sub-strings out of them
+\item Reverse strings
+\item Replace characters in strings.
+\item Convert strings to upper or lower case
+\item Join a list of strings
+\end{itemize}
+\end{frame}
+\begin{frame}
+\frametitle{Thank you!}
+\label{sec-11}
+
+ \begin{block}{}
+ \begin{center}
+ This spoken tutorial has been produced by the
+ \textcolor{blue}{FOSSEE} team, which is funded by the
+ \end{center}
+ \begin{center}
+ \textcolor{blue}{National Mission on Education through \\
+ Information \& Communication Technology \\
+ MHRD, Govt. of India}.
+ \end{center}
+ \end{block}
+\end{frame}
+
+\end{document}