<!DOCTYPE html>
<html>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="css/jquery.mobile-1.4.4.min.css">
		<link rel="stylesheet" href="css/style.css">
		<script src="js/jquery-1.11.1.min.js"></script>
		<script src="js/jquery.mobile-1.4.4.min.js"></script>

		<link rel="stylesheet" href="js/prettify/desert.css">
		<script src="js/prettify/prettify.js"></script>
		
	</head>
	<body onload="prettyPrint()">

		<div data-role="page" id="main" data-theme="b">
			<!-- For the topics panel -->

			<div id="pnlTopic" data-role="panel" data-display="overlay" data-swipe-close="true" data-dismissible="true" data-animate="true">
				<h2>Topics</h2>
				<ul data-role="listview">
					<li>
						<a href="variable.htm" rel="external">Variable</a>
					</li>
					<li>
						<a href="conditional.htm" rel="external">Conditional</a>
					</li>
					<li>
						<a href="function.htm" rel="external">Function</a>

					</li>
					<li>
						<a href="forLoop.htm" rel="external">For Loop</a>

					</li>
					<li>
						<a href="whileLoop.htm" rel="external">While Loop</a>

					</li>
					<li>
						<a href="list.htm" rel="external">List</a>

					</li>
					<li>
						<a href="string.htm" rel="external">String</a>
					</li>
					<li>
						<a href="dictionary.htm" rel="external">Dictionary</a>
					</li>
					<li>
						<a href="tuple.htm" rel="external">Tuple</a>
					</li>
					<li>
						<a href="challenge.htm" rel="external"><img src="images/challenge36.png" class="ui-li-icon">Python Challenge</a>
					</li>
				</ul>
			</div>

			<div data-role="header">
				<h1>String</h1>
				<a href="#pnlTopic" class="ui-btn ui-icon-grid ui-btn-icon-left ui-btn-icon-notext"></a>
				<a href="index.htm" rel="external" class="ui-btn ui-icon-home ui-btn-icon-right ui-btn-icon-notext"></a>
			</div>

			<div data-role="main" class="ui-content">
				<div data-role="collapsibleset">
					<div data-role="collapsible">
						<h3>String Creation</h3>
						<p>
							String is one of the most common used data type in Python. You create a string by enclosing some characters or numbers in single (') or double (") quotes.
						</p>
						<pre class="prettyprint">

# using double quote
>>> a = "hello"

# using single quote             
>>> b = 'world'         

# concatenates string    
>>> a + b                  
'helloworld'

# query for data type
>>> type(a)                 
&lt;type 'str'&gt;

# mixing single and double quotes
>>> c = "How're you doing?" 
>>> c
"How're you doing?"

>>> d = """ A long string
... that spans a few
... lines can be enclosed in
... triple quotes.
... """

>>> d
' A long string \nthat spans a few\nlines can be enclosed in \ntriple quotes.\n'             
						</pre>
					</div>
					<div data-role="collapsible">
						<h3>Accessing Values in a String</h3>
						<p>
							A <em>string</em> comprises of a sequence of characters, and quite a few methods/functions that work on list is applicable to String as well.
						</p>
						<pre class="prettyprint">

>>> a = "hello"

# get first character (same as list, zero indexed)
>>> a[0]                
'h'

# get get last character
>>> a[-1]               
'o'

# slicing works on string as well
>>> a[0:2]              
'he'

# get number of characters in the string
>>> len(a)              
5

# get the number of occurence of a particular character
>>> a.count('l')        
2      
						</pre>
					</div>
					<div data-role="collapsible">
						<h3>Modifying a String</h3>
						<p>
							String is immutable, and therefore its value cannot be modified directly. However, you can "modify" a string by re-assigning the value to another string.
						</p>
						<pre class="prettyprint">

>>> a = "hello"

# Not allow, produce error
>>> a[0] = 'a'          
Traceback (most recent call last):
  File "&lt;interactive input&gt;", line 1, in &lt;module&gt;
TypeError: 'str' object does not support item assignment

# replace the first character with 'a', and reassign the result to a new string
>>> b = 'a' + a[1:]     
>>> b
'aello'
						</pre>		
					</div>
					<div data-role="collapsible">
						<h3>Built-in String Methods</h3>
						<p>
							Python provides quite a few very handy string methods. Some commonly used ones are shown below.
						</p>
						<pre class="prettyprint">

>>> a = "hello"
>>> a.capitalize()
'Hello'
>>> a.upper()
'HELLO'
>>> a.lower()
'hello'
>>> a.replace("e", "a")
'hallo'               
1
						</pre>		
					</div>
					<div data-role="collapsible">
						<h3>Methods for Finding Substrings</h3>
						<pre class="prettyprint">

>>> a = "Hello World"
>>> a.startswith("H")
True
>>> a.endswith("D")
False
>>> a.find("World")
6
>>> a.index("World")
6

# 'find' returns -1 if no substring is found.
>>> a.find("world") 
-1

# 'index' returns a error
>>> a.index("world") 
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ValueError: substring not found
						</pre>		
					</div>
					<div data-role="collapsible">
						<h3>Code Snippets</h3>
						<p>
							This section will contain some code snippets to show some practical use of the topic that you have learnt. 
						</p>
						<pre class="prettyprint">

# Doing a Word Count
# ----------------------------------
sentence = "This sentence contains some words"

# Use split() function to breakdown the sentence into a word list
wordList = sentence.split()

# Use len() function to count number of items in the list
wordCount = len(wordList)

print "Number of words: ", wordCount

# output
Number of words:  5

# Checking for 4 digit number
# ----------------------------------
def isFourDigitNumber(x):
    if len(x) == 4 and x.isdigit():
        return True
    else:
        return False

print isFourDigitNumber("2345") # True
print isFourDigitNumber("23456") # False
print isFourDigitNumber("123a") # True
						</pre>
					</div>
				</div>
			</div>
			
		</div>

	</body>
</html>