<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FManipulating-strings_%2FEnglish</id>
		<title>Python/C3/Manipulating-strings /English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FManipulating-strings_%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Manipulating-strings_/English&amp;action=history"/>
		<updated>2026-04-08T19:59:53Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C3/Manipulating-strings_/English&amp;diff=505&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello Friends and Welcome to this tutorial on …'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Manipulating-strings_/English&amp;diff=505&amp;oldid=prev"/>
				<updated>2012-11-29T06:19:55Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello Friends and Welcome to this tutorial on …&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello Friends and Welcome to this tutorial on 'manipulating strings'.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Slice strings and get sub-strings out of them.&lt;br /&gt;
# Reverse strings.&lt;br /&gt;
# Replace characters in strings.&lt;br /&gt;
# Convert strings to upper or lower case.&lt;br /&gt;
# Join a list of strings.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Pre-requisite slide &lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;getting started with strings&amp;quot;, &amp;quot;getting started with lists&amp;quot; and &amp;quot;basic datatypes and operators&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open the terminal &lt;br /&gt;
&lt;br /&gt;
 ipython&lt;br /&gt;
| Let us invoke our ipython interpreter&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;week = [&amp;quot;sun&amp;quot;, &amp;quot;mon&amp;quot;, &amp;quot;tue&amp;quot;, &amp;quot;wed&amp;quot;, &amp;quot;thu&amp;quot;, &amp;quot;fri&amp;quot;, &amp;quot;sat&amp;quot;]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| Let us consider a simple problem, and learn how to slice strings and get sub-strings.&lt;br /&gt;
&lt;br /&gt;
Let's say the variable &amp;lt;tt&amp;gt;week&amp;lt;/tt&amp;gt; has the list of the names of the days of the week.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  s = &amp;quot;saturday&amp;quot;&lt;br /&gt;
| Now given a string &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt;, we should be able to check if the string is a valid name of a day of the week or not. Let us define our string as,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
Forms &lt;br /&gt;
| &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt; could be in any of the forms --- sat, saturday, Sat, Saturday, SAT, SATURDAY. For now, we shall 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.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;s[0:3]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| So, we need to check if the first three characters of the given string exists in the variable &amp;lt;tt&amp;gt;week&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
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,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Note that, we are slicing the string from the index 0 to index 3, 3 not included.&lt;br /&gt;
&lt;br /&gt;
As we already know, the last element of the string can be accessed using &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;s[-1]&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| '''Obtain the sub-string excluding the first and last characters'''&lt;br /&gt;
&lt;br /&gt;
from the string s.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;s[1:-1]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| Switch to the terminal for solution&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;s[:3]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;s[:3] in week&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| We get the substring of s, without the first and the last characters of s.&lt;br /&gt;
&lt;br /&gt;
Now let us check if a particular substring is present in the variable &amp;lt;tt&amp;gt;week&amp;lt;/tt&amp;gt;. We shall check for 'sat'.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  s1 = &amp;quot;malayalam&amp;quot;&lt;br /&gt;
| We get the result as true. 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.&lt;br /&gt;
&lt;br /&gt;
Let the string given be &amp;lt;tt&amp;gt;malayalam&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;s1[::-1]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| Now, we need to compare this string with it's reverse.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;Again, we will use a technique common to all sequence data-types, that is, [::-1]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, we obtain the reverse of s, by simply saying,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;s1 == s1[::-1]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| Now, to check if the string is &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt; is palindromic, we say&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  s1 = &amp;quot;Malayalam&amp;quot;&lt;br /&gt;
 s1.upper()&lt;br /&gt;
 s1&lt;br /&gt;
| As, expected, we get &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, if the string we are given is &amp;lt;tt&amp;gt;Malayalam&amp;lt;/tt&amp;gt; instead of &amp;lt;tt&amp;gt;malayalam&amp;lt;/tt&amp;gt;, the above comparison would return a False. So, we will have to convert the string to all lower case or to all upper case, before comparing. Python provides methods, &amp;lt;tt&amp;gt;s.lower&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;s.upper&amp;lt;/tt&amp;gt; to achieve this.&lt;br /&gt;
&lt;br /&gt;
Let's try it out.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  s1.lower()&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;s1.lower() == s1.lower()[::-1]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| As you can see, s has not changed. It is because, &amp;lt;tt&amp;gt;upper&amp;lt;/tt&amp;gt; returns a new string. It doesn't change the original string. Similarly,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Assignment 2 &lt;br /&gt;
| '''Check if &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt; is a valid name of a day of the week. Change the'''&lt;br /&gt;
&lt;br /&gt;
solution to this problem, to include forms like, SAT, SATURDAY, Saturday and Sat.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 s in week&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;nowiki&amp;gt;s.lower()[:3] in week&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| Switch to your terminal for solution&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;email = &amp;quot;info[at]fossee[dot]in&amp;quot;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| So, as you can see, now we can check for presence of &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;week&amp;lt;/tt&amp;gt;, in whichever format it is present -- capitalized, or all caps, full name or short form.&lt;br /&gt;
&lt;br /&gt;
We just convert any input string to lower case and then check if it is present in the list &amp;lt;tt&amp;gt;week&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;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.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's say the variable email has the email address.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;email = email.replace(&amp;quot;[at]&amp;quot;, &amp;quot;@&amp;quot;)&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 print email&lt;br /&gt;
| Now, we first replace the &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;[at]&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; with the &amp;lt;tt&amp;gt;@&amp;lt;/tt&amp;gt;, using the replace method of strings.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7 &lt;br /&gt;
&lt;br /&gt;
Assignment 3 &lt;br /&gt;
| Replace the &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;[dot]&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; with &amp;lt;tt&amp;gt;.&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;email&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;email = email.replace(&amp;quot;[dot]&amp;quot;, &amp;quot;.&amp;quot;)&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 print email&lt;br /&gt;
| Switch to the terminal for solution&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;email_list = [&amp;quot;info@fossee.in&amp;quot;, &amp;quot;enquiries@fossee.in&amp;quot;, &amp;quot;help@fossee.in&amp;quot;]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| Now, let us 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.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  email_str = &amp;quot;, &amp;quot;.join(email_list)&lt;br /&gt;
 print email_str&lt;br /&gt;
| Now, if we wish to obtain one long string, separating each of the email id by a comma, we use the join operator on &amp;lt;tt&amp;gt;,&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Notice that the email ids are joined by a comma followed by a space.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
Assignment 4 &lt;br /&gt;
| '''From the email_str that we generated, change the separator to be'''&lt;br /&gt;
&lt;br /&gt;
a semicolon instead of a comma.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 email_str = email_str.replace(&amp;quot;,&amp;quot;, &amp;quot;;&amp;quot;)&lt;br /&gt;
 print email_str&lt;br /&gt;
| Switch to the terminal for solution&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| We see that the email ids are joined by a semicolon followed by a space.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 9 &lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of this tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Obtain sub-strings and reverse of strings by using the index numbers&lt;br /&gt;
# Use following functions - - &amp;lt;tt&amp;gt;upper()&amp;lt;/tt&amp;gt; -- to obtain the upper case of a string - &amp;lt;tt&amp;gt;lower()&amp;lt;/tt&amp;gt; -- to obtain the lower case of a string - &amp;lt;tt&amp;gt;replace()&amp;lt;/tt&amp;gt; -- to replace a character by another one - &amp;lt;tt&amp;gt;join()&amp;lt;/tt&amp;gt; -- to join a list of strings with an operator&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 10&lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# Given a string &amp;lt;tt&amp;gt;s = &amp;quot;this is a string&amp;quot;&amp;lt;/tt&amp;gt;, how will you change it to &amp;lt;tt&amp;gt;&amp;quot;this isn't a list&amp;quot;&amp;lt;/tt&amp;gt; ?&lt;br /&gt;
# Given the string &amp;quot;F.R.I.E.N.D.S&amp;quot; in s, obtain the string &amp;quot;friends&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 11&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
1. We will use the &amp;lt;tt&amp;gt;replace&amp;lt;/tt&amp;gt; function to accomplish this.&lt;br /&gt;
&lt;br /&gt;
 s = s.replace(&amp;quot;string&amp;quot;, &amp;quot;list&amp;quot;)&lt;br /&gt;
 s = s.replace(&amp;quot;is&amp;quot;, &amp;quot;isn't&amp;quot;)&lt;br /&gt;
 s&lt;br /&gt;
&lt;br /&gt;
We notice that every 'is' in the statement has been replaced by &amp;lt;tt&amp;gt;isn't&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
# In order to change the string to lower case, we use the method &amp;lt;tt&amp;gt;lower()&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enumerated list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;s[::2].lower()&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 12 &lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>