diff options
Diffstat (limited to 'getting_started_with_strings')
-rw-r--r-- | getting_started_with_strings/quickref.tex | 8 | ||||
-rw-r--r-- | getting_started_with_strings/script.rst | 191 | ||||
-rw-r--r-- | getting_started_with_strings/slides.org | 80 | ||||
-rw-r--r-- | getting_started_with_strings/slides.tex | 129 |
4 files changed, 408 insertions, 0 deletions
diff --git a/getting_started_with_strings/quickref.tex b/getting_started_with_strings/quickref.tex new file mode 100644 index 0000000..3bd7055 --- /dev/null +++ b/getting_started_with_strings/quickref.tex @@ -0,0 +1,8 @@ +Creating a string:\\ +{\ex \lstinline| s = ``Hello World''|} -- Single quotes and triple +quotes can also be used. + +Accessing individual elements:\\ +{\ex \lstinline| s[5]|} -- Elements can be accessed with their index + +Strings are immutable. diff --git a/getting_started_with_strings/script.rst b/getting_started_with_strings/script.rst new file mode 100644 index 0000000..754fede --- /dev/null +++ b/getting_started_with_strings/script.rst @@ -0,0 +1,191 @@ +.. Objectives +.. ---------- + +.. At the end of this tutorial, you should know -- + +.. 1. How to define strings +.. #. Different ways of defining a string +.. #. How to concatenate strings +.. #. How to print a string repeatedly +.. #. Accessing individual elements of the string +.. #. Immutability of strings + +.. Prerequisites +.. ------------- + +.. 1. getting started with ipython + +.. Author : Madhu + Internal Reviewer : Punch + External Reviewer : + Language Reviewer : Bhanukiran + Checklist OK? : <15-11-2010, Anand, OK> [2010-10-05] + +Script +------ + +{{{ Show the slide containing the title }}} + +Hello friends. Welcome to this spoken tutorial on Getting started with +strings. + +{{{ Show the slide containing the outline }}} + +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. + +{{{ Shift to terminal and start ipython }}} + +To begin with let us start ipython, by typing:: + + ipython + +on the terminal + +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 }}}:: + + 'This is a string' + "This is a string too' + '''This is a string as well''' + """This is also a string""" + 'p' + "" + +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:: + + "Python's string manipulation functions are very useful" + +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 +using any escaping. Everything within the triple quotes is a single +string no matter how many lines it extends:: + + """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.""" + +We can assign this string to any variable:: + + a = 'Hello, World!' + +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:: + + a = 'Hello' + b = 'World' + c = a + ', ' + b + '!' + +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:: + + a = 'Hello' + a * 5 + +gives another string in which the original string 'Hello' is repeated +5 times. + +Following is an exercise that you must do. + +%% %% Obtain the string ``%% -------------------- %%`` (20 hyphens) + without typing out all the twenty hyphens. + +Please, pause the video here. Do the exercise and then continue. + +:: + + s = "%% " + "-"*20 + " %%" + +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:: + + 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. We +can access the strings from the end using negative indices:: + + a[-1] + +gives us the last element of the string and +:: + + a[-2] + +gives us second element from the end of the string + +Following is an exercise that you must do. + +%% %% Given a string, ``s = "Hello World"``, what is the output of:: + + s[-5] + s[-10] + s[-15] + +Please, pause the video here. Do the exercise and then continue. + +:: + + s[-5] + +gives us 'W' +:: + + s[-10] + +gives us 'e' and +:: + + 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:: + + a = 'hello' + a[0] = 'H' + +As said earlier, strings are immutable. We cannot manipulate a +string. Although there are some methods which let us manipulate +strings, we will look at them in the advanced session on strings. In +addition to the methods that let us manipulate the strings we have +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. + +{{{ Show summary slide }}} + +This brings us to the end of another session. In this tutorial session +we learnt + + * How to define strings + * Different ways of defining a string + * String concatenation and repetition + * Accessing individual elements of the string + * Immutability of strings + +{{{ 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/getting_started_with_strings/slides.org b/getting_started_with_strings/slides.org new file mode 100644 index 0000000..a1df437 --- /dev/null +++ b/getting_started_with_strings/slides.org @@ -0,0 +1,80 @@ +#+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: +#+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 +*** Defining strings +*** Concatenation +*** Accessing individual elements +*** 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 + s[-5] + 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 + \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/getting_started_with_strings/slides.tex b/getting_started_with_strings/slides.tex new file mode 100644 index 0000000..ed0dedb --- /dev/null +++ b/getting_started_with_strings/slides.tex @@ -0,0 +1,129 @@ +% Created 2010-11-10 Wed 10:46 +\documentclass[presentation]{beamer} +\usepackage[latin1]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{fixltx2e} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{float} +\usepackage{wrapfig} +\usepackage{soul} +\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{} +\author{FOSSEE} +\date{} + +\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +\begin{document} + + + + + + + + + + + +\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 +\end{frame} +\begin{frame} +\frametitle{Question 1} +\label{sec-2} + + Obtain the string \texttt{\%\% -------------------- \%\%} (20 hyphens) without + typing out all the twenty hyphens. +\end{frame} +\begin{frame}[fragile] +\frametitle{Solution 1} +\label{sec-3} + +\lstset{language=Python} +\begin{lstlisting} +s = "%% " + "-"*20 + " %%" +\end{lstlisting} +\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:: +\lstset{language=Python} +\begin{lstlisting} +s[-5] +s[-10] +s[-15] +\end{lstlisting} +\end{frame} +\begin{frame}[fragile] +\frametitle{Solution 2} +\label{sec-5} + +\lstset{language=Python} +\begin{lstlisting} +'W' +'e' +IndexError +\end{lstlisting} +\end{frame} +\begin{frame} +\frametitle{Summary} +\label{sec-6} + + In this tutorial we have learnt +\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 +\end{itemize} + + +\end{frame} +\begin{frame} +\frametitle{Thank you!} +\label{sec-7} + + \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} |