diff options
Diffstat (limited to 'getting_started_with_strings')
-rw-r--r-- | getting_started_with_strings/script.rst | 293 | ||||
-rw-r--r-- | getting_started_with_strings/slides.org | 88 | ||||
-rw-r--r-- | getting_started_with_strings/slides.tex | 116 |
3 files changed, 344 insertions, 153 deletions
diff --git a/getting_started_with_strings/script.rst b/getting_started_with_strings/script.rst index 754fede..d383dcb 100644 --- a/getting_started_with_strings/script.rst +++ b/getting_started_with_strings/script.rst @@ -24,144 +24,242 @@ Script ------ -{{{ Show the slide containing the title }}} +.. L1 -Hello friends. Welcome to this spoken tutorial on Getting started with -strings. +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} -{{{ Show the slide containing the outline }}} +.. R1 -In this tutorial, we will look at what we really mean by strings, how -Python supports the use of strings and some of the operations that can -be performed on strings. +Hello friends and Welcome to the tutorial on "Getting started with +strings". + +.. L2 + +{{{ Show slide with objectives }}} + +.. R2 + +At the end of this tutorial, you will be able to, + + 1. Define strings in differnt ways. + #. Concatenate strings. + #. Print a string repeatedly. + #. Access individual elements of the string. + #. Learn immutability of strings. + +.. L3 {{{ Shift to terminal and start ipython }}} +:: -To begin with let us start ipython, by typing:: + ipython - ipython +.. R3 -on the terminal +Open the terminal and invoke the ipython interpreter by typing ipython + +.. R4 So, what are strings? In Python anything within either single quotes or double quotes or triple single quotes or triple double quotes are strings. -{{{ Type in ipython the following and read them as you type }}}:: +.. L4 + +{{{ Type in ipython the following and read them as you type }}} +:: + + 'This is a string' + "This is a string too" + '''This is a string as well''' + """This is also a string""" + 'p' + "" - 'This is a string' - "This is a string too' - '''This is a string as well''' - """This is also a string""" - 'p' - "" +.. R5 Note that it really doesn't matter how many characters are present in the string. The last example is a null string or an empty string. Having more than one control character to define strings is handy when -one of the control characters itself is part of the string. For -example:: +one of the control characters itself is part of the string. For example + +.. L5 + +:: - "Python's string manipulation functions are very useful" + "Python's string manipulation functions are very useful" + +.. R6 By having multiple control characters, we avoid the need for escaping characters -- in this case the apostrophe. -The triple quoted strings let us define multi-line strings without +Let us now move on to the triple quoted strings. Let us define multi-line strings without using any escaping. Everything within the triple quotes is a single -string no matter how many lines it extends:: +string no matter how many lines it extends + +.. L6 +:: + + """Having more than one control character to define + strings come as very handy when one of the control + characters itself is part of the string.""" - """Having more than one control character to define - strings come as very handy when one of the control - characters itself is part of the string.""" +.. R7 -We can assign this string to any variable:: +We can assign this string to any variable - a = 'Hello, World!' +.. L7 +:: + + a = 'Hello, World!' + +.. R8 Now 'a' is a string variable. String is a collection of characters. In -addition string is an immutable collection. So all the operations that -are applicable to any other immutable collection in Python works on -string as well. So we can add two strings:: +addition string is an immutable collection which means that the string cannot be modified +after it is created.So all the operations that are applicable to any other immutable +collection in Python, works on strings as well. Hence we can add two strings + +.. L8 +:: - a = 'Hello' - b = 'World' - c = a + ', ' + b + '!' + a = 'Hello' + b = 'World' + c = a + ', ' + b + '!' + print c + +.. R9 We can add string variables as well as the strings themselves all in the same statement. The addition operation performs the concatenation of two strings. -Similarly we can multiply a string with an integer:: +.. L9 + +.. R10 + +Similarly we can multiply a string with an integer + +.. L10 +:: + + a = 'Hello' + a * 5 - a = 'Hello' - a * 5 +.. R11 -gives another string in which the original string 'Hello' is repeated +It gives another string in which the original string 'Hello' is repeated 5 times. -Following is an exercise that you must do. +.. L11 + +.. L12 + +{{{ Show slide with Question 1 }}} + +.. R12 -%% %% Obtain the string ``%% -------------------- %%`` (20 hyphens) - without typing out all the twenty hyphens. +Pause the video here, try out the following exercise and resume the video. -Please, pause the video here. Do the exercise and then continue. + Obtain the string ``%% -------------------- %%`` (20 hyphens) + without typing out all the twenty hyphens. + +.. L13 + +{{{ Switch to terminal }}} :: - s = "%% " + "-"*20 + " %%" + s = "%% " + "-"*20 + " %%" + print s + +.. R13 Let's now look at accessing individual elements of strings. Since, -strings are collections we can access individual items in the string -using the subscripts:: +strings are collections, we can access individual items in the string +using the subscripts - a[0] +.. L14 +:: -gives us the first character in the string. The indexing starts from 0 -for the first character and goes up to n-1 for the last character. We -can access the strings from the end using negative indices:: + a[0] - a[-1] +.. R14 -gives us the last element of the string and +a[0] gives us the first character in the string. The indexing starts from 0 +for the first character and goes up to (n-1) for the last character,where 'n' is the total +number of characters in a string. +We can access the strings from the end using negative indices + +.. L15 :: + a[-1] a[-2] -gives us second element from the end of the string +.. R15 + +a[-1] gives us the last element of the string and +a[-2] gives us second element from the end of the string. + +.. L16 + +{{{ Show slide with Question 2 }}} + +.. R16 -Following is an exercise that you must do. +Pause the video here, try out the following exercise and resume the video. -%% %% Given a string, ``s = "Hello World"``, what is the output of:: +Given a string, ``s = "Hello World"``, what is the output of:: - s[-5] - s[-10] - s[-15] + s[-5] + s[-10] + s[-15] -Please, pause the video here. Do the exercise and then continue. +.. L17 +{{{ Switch to terminal }}} :: - s[-5] + s[-5] -gives us 'W' +.. R17 + +s[-5] gives us 'W' + +.. L18 :: - s[-10] + s[-10] + +.. R18 + +s[-10] gives us 'e' and -gives us 'e' and +.. L19 :: - s[-15] + s[-15] -gives us an ``IndexError``, as should be expected, since the string +.. R19 + +s[-15] gives us an ``IndexError``, as should be expected, since the string given to us is only 11 characters long. -Let us attempt to change one of the characters in a string:: +.. R20 + +Let us attempt to change one of the characters in a string + +.. L20 +:: + + a = 'hello' + a[0] = 'H' - a = 'hello' - a[0] = 'H' +.. R21 As said earlier, strings are immutable. We cannot manipulate a string. Although there are some methods which let us manipulate @@ -171,20 +269,65 @@ methods like split which lets us break the string on the specified separator, the join method which lets us combine the list of strings into a single string based on the specified separator. +.. L21 + +.. L22 + {{{ Show summary slide }}} -This brings us to the end of another session. In this tutorial session -we learnt +.. R22 + +Let's revise quickly what we have learnt today.In this tutorial we have learnt to, + + 1. Define strings in differnt ways. + #. Concatenate strings by performing addition. + #. Repeat a string 'n' number of times by doing multiplication. + #. Access individual elements of the string by using their subscripts. + #. Use the concept of immutability of strings. + +.. L23 + +{{{Show self assessment questions slide}}} + +.. R23 + +Here are some self assessment questions for you to solve + +1. Write code to assign s, the string ``' is called the apostrophe`` + +2. Given strings s and t, ``s = "Hello"`` and ``t = "World"`` and an + integer r, ``r = 2``. What is the output of s * r + s * t? + +3. How will you change s='hello' to s='Hello'. + + - s[0]= H + - s[0]='H' + - strings are immutable,hence cannot be manipulated + +.. L24 + +{{{ solution of self assessment questions on slide }}} + +.. R24 + +And the answers, + +1. The given string can be assigned in this manner +:: + + s = "` is called the apostrophe" + +2. The operation ``s * r + s * t`` will print each of the two words twice + + HelloHelloWorldWorld + +3. Strings are immutable.Therefore they cannot be manipulated. - * How to define strings - * Different ways of defining a string - * String concatenation and repetition - * Accessing individual elements of the string - * Immutability of strings +.. L25 -{{{ Show the "sponsored by FOSSEE" slide }}} +{{{ Show the Thankyou slide }}} -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India +.. R25 Hope you have enjoyed and found it useful. Thank you! diff --git a/getting_started_with_strings/slides.org b/getting_started_with_strings/slides.org index a1df437..cd95474 100644 --- a/getting_started_with_strings/slides.org +++ b/getting_started_with_strings/slides.org @@ -29,18 +29,33 @@ #+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 -*** Defining strings -*** Concatenation -*** Accessing individual elements -*** Immutability of strings +* + #+begin_latex +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Getting started with Strings} +\end{center} +\vspace{18pt} +\begin{center} +\vspace{10pt} +\includegraphics[scale=0.95]{../images/fossee-logo.png}\\ +\vspace{5pt} +\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ +\scriptsize Funded by National Mission on Education through ICT\\ +\scriptsize MHRD,Govt. of India\\ +\includegraphics[scale=0.30]{../images/iitb-logo.png}\\ +\end{center} +#+end_latex +* Objectives + At the end of this tutorial, you will be able to, + - Define strings in differnt ways. + - Concatenate strings. + - Print a string repeatedly. + - Access individual elements of the string. + - Learn immutability of strings. * Question 1 Obtain the string ~%% -------------------- %%~ (20 hyphens) without typing out all the twenty hyphens. -* Solution 1 - #+begin_src python - s = "%% " + "-"*20 + " %%" - #+end_src * Question 2 Given a string, ~s~ which is ~Hello World~ , what is the output of:: #+begin_src python @@ -48,33 +63,44 @@ s[-10] s[-15] #+end_src -* Solution 2 - #+begin_src python - 'W' - 'e' - IndexError - #+end_src * Summary - In this tutorial we have learnt - + How to define strings - + Different ways of defining a string - + String concatenation and repetition - + Accessing individual elements of the string - + Immutability of strings - -* Thank you! -#+begin_latex + In this tutorial, we have learnt, + - To define strings in differnt ways. + - To concatenate strings by performing addition. + - To repeat a string 'n' number of times by doing multiplication. + - To access individual elements of the string by using their subscripts. + - Use the concept of immutability of strings. + +* Evaluation + 1. Write code to assign s, the string ``' is called the apostrophe`` + + 2. Given strings s and t, ``s = "Hello"`` and ``t = "World"`` and an + integer r, ``r = 2``. What is the output of s * r + s * t? + + 3. How will you change s='hello' to s='Hello'. + + - s[ 0 ]= H + - s[ 0 ]='H' + - strings are immutable,hence cannot be manipulated. +* Solutions + 1. s = "` is called the apostrophe" + + 2. HelloHelloWorldWorld + + 3. Strings are immutable,hence cannot be manipulated. + +* + #+begin_latex \begin{block}{} \begin{center} - This spoken tutorial has been produced by the - \textcolor{blue}{FOSSEE} team, which is funded by the + \textcolor{blue}{\Large THANK YOU!} \end{center} + \end{block} +\begin{block}{} \begin{center} - \textcolor{blue}{National Mission on Education through \\ - Information \& Communication Technology \\ - MHRD, Govt. of India}. + For more Information, visit our website\\ + \url{http://fossee.in/} \end{center} \end{block} #+end_latex - - + diff --git a/getting_started_with_strings/slides.tex b/getting_started_with_strings/slides.tex index ed0dedb..e38ca13 100644 --- a/getting_started_with_strings/slides.tex +++ b/getting_started_with_strings/slides.tex @@ -1,4 +1,4 @@ -% Created 2010-11-10 Wed 10:46 +% Created 2011-05-16 Mon 12:57 \documentclass[presentation]{beamer} \usepackage[latin1]{inputenc} \usepackage[T1]{fontenc} @@ -41,41 +41,48 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries} \begin{frame} -\frametitle{Outline} -\label{sec-1} -\begin{itemize} -\item Defining strings\\ -\label{sec-1_1}% -\item Concatenation\\ -\label{sec-1_2}% -\item Accessing individual elements\\ -\label{sec-1_3}% -\item Immutability of strings\\ -\label{sec-1_4}% -\end{itemize} % ends low level +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Getting started with Strings} +\end{center} +\vspace{18pt} +\begin{center} +\vspace{10pt} +\includegraphics[scale=0.95]{../images/fossee-logo.png}\\ +\vspace{5pt} +\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ +\scriptsize Funded by National Mission on Education through ICT\\ +\scriptsize MHRD,Govt. of India\\ +\includegraphics[scale=0.30]{../images/iitb-logo.png}\\ +\end{center} \end{frame} \begin{frame} -\frametitle{Question 1} +\frametitle{Objectives} \label{sec-2} - Obtain the string \texttt{\%\% -------------------- \%\%} (20 hyphens) without - typing out all the twenty hyphens. + At the end of this tutorial, you will be able to, + +\begin{itemize} +\item Define strings in differnt ways. +\item Concatenate strings. +\item Print a string repeatedly. +\item Access individual elements of the string. +\item Learn immutability of strings. +\end{itemize} \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 1} +\begin{frame} +\frametitle{Question 1} \label{sec-3} -\lstset{language=Python} -\begin{lstlisting} -s = "%% " + "-"*20 + " %%" -\end{lstlisting} + Obtain the string \verb~%% -------------------- %%~ (20 hyphens) without + typing out all the twenty hyphens. \end{frame} \begin{frame}[fragile] \frametitle{Question 2} \label{sec-4} - Given a string, \texttt{s} which is \texttt{Hello World} , what is the output of:: + Given a string, \verb~s~ which is \verb~Hello World~ , what is the output of:: \lstset{language=Python} \begin{lstlisting} s[-5] @@ -83,47 +90,62 @@ s[-10] s[-15] \end{lstlisting} \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 2} +\begin{frame} +\frametitle{Summary} \label{sec-5} -\lstset{language=Python} -\begin{lstlisting} -'W' -'e' -IndexError -\end{lstlisting} + In this tutorial, we have learnt, + +\begin{itemize} +\item To define strings in differnt ways. +\item To concatenate strings by performing addition. +\item To repeat a string `n' number of times by doing multiplication. +\item To access individual elements of the string by using their subscripts. +\item About the immutability of strings. +\end{itemize} \end{frame} \begin{frame} -\frametitle{Summary} +\frametitle{Evaluation} \label{sec-6} - In this tutorial we have learnt + +\begin{enumerate} +\item Write code to assign s, the string ``' is called the apostrophe`` +\item Given strings s and t, ``s = ``Hello''`` and ``t = ``World''`` and an + integer r, ``r = 2``. What is the output of s * r + s * t? +\item How will you change s='hello' to s='Hello'. \begin{itemize} -\item How to define strings -\item Different ways of defining a string -\item String concatenation and repetition -\item Accessing individual elements of the string -\item Immutability of strings +\item s[ 0 ]= H +\item s[ 0 ]='H' +\item strings are immutable,hence cannot be manipulated. \end{itemize} - - +\end{enumerate} \end{frame} \begin{frame} -\frametitle{Thank you!} +\frametitle{Solutions} \label{sec-7} + +\begin{enumerate} +\item s = ``` is called the apostrophe'' +\item HelloHelloWorldWorld +\item Strings are immutable,hence cannot be manipulated. +\end{enumerate} +\end{frame} +\begin{frame} + \begin{block}{} \begin{center} - This spoken tutorial has been produced by the - \textcolor{blue}{FOSSEE} team, which is funded by the + \textcolor{blue}{\Large THANK YOU!} \end{center} + \end{block} +\begin{block}{} \begin{center} - \textcolor{blue}{National Mission on Education through \\ - Information \& Communication Technology \\ - MHRD, Govt. of India}. + For more Information, visit our website\\ + \url{http://fossee.in/} \end{center} \end{block} + \end{frame} -\end{document} +\end{document}
\ No newline at end of file |