Wednesday 7 January 2015

My Python Notes:Strings

What are strings ?

String is a collection of numbers, characters or both within single or double quotes.

Open the python IDLE and type the following lines in the shell
Example - 1


Once executed the output is
Now when we need to print a statement having single quotes , we have to enclose it within a double quotes and vice-versa.

An example for the above statement is as follows type the following lines in Python IDLE.
Example - 2


Once executed the output is

Combining Strings

We will combine two strings.
Example - 3 ---save it in ex3.py


Output




String are not Mutable
Consider string name="jude"
The element at name[0] is 'j'  we cannot change its value.
 
But we can make the string "name" point to a new string
Eg---- name ="augustine"
 

 Slice  the String

Let us see how string are stored in the computer, with their respective indices.

index: a position of element within the string
From the above diagram it is clear that the first element has an index of Zero,
and the last element has an index of (length of string - 1) where length of string is the number of characters ie 5.
Python also allow us to access the last element using the index as -1 and the second last element as
-2 and so on.

Example 4 -- lets write program to print element based on the index value.



Slicing strings
Slice: is a substring of a string from a start index up to but not including an end index.
Eg str_new = str1[1:3] ie str_new will contain element from index 1 to 3 and not including 3.
Let us consider a string
blogName = "pythonselenium.blogspot.com"


If i only want to print the first 6 letter only.

language = blogName[0:6]
# we print from 0 up-to element in the index = 6 but not including the element at index = 6.

If i want to print the word "selenium" from the string "blogName".
tool = blogName[6:14]

If i want to print the last three character from the string "blogName"
str1 = blogName[-1:-4]

Example 5--slice the string



Output

String Methods

Python is object-oriented language.Every piece of data and even functions and types are objects.
String is also an object, function within a object are called Methods.

Lets us examine some string methods.In order to find all the method for string object,
we can type "dir(str)" in the IDLE shell.


To learn more about a particular method we can use "help(str.method_name)"
lets find more about swapcase()--method.


quote = "Talent is God given.Be humble.Fame is man-given.Be grateful.Conceit is self-given.Be careful."

If we want to change the above string quote to lower-case letter we can use the "lower()" --method.
To find the number of occurrence of the sub-string in the main string we can use "count()"--method.


 Loops

Loops are used when we want to do a repetitive task.In python we have two type of loops namely
"for" and "while" loops.

for---loops

Before we write the code we need to learn about another method called "range()" in python.
Eg-- range(4) will return a sequence from 0 to 3 ie...0,1,2,3.
In general range(n) will return a sequence from 0 to n-1.

Example 6--print number from 0 to 10 using range() and for--loop.





Example 7.1
Problem: Consider a string name="JUDE JOHN"
I want to print character by character ,lets us do it using for --loop.
Answer: 

 len(name) = 9 so range(len(name)) will return a sequence from 0 to 8

i = 0  name[i] is name[0] = J
i = 1  name[i] is name[1] = U
i = 2  name[i] is name[2] = D
i = 3  name[i] is name[3] = E
i = 4  name[i] is name[4] = " "
i = 5  name[i] is name[5] = J
i = 6  name[i] is name[6] = O 
i = 7  name[i] is name[7] = H
i = 8  name[i] is name[8] = N

We are iterating using the indices.


Output

The above code can be re-written as

Example 7.2--In python we simply iterate over the values directly,
 will result in the same result as in example 7.1


Example 8--Count the number of vowels in a given string.



Example 9--Collecting the vowels in a given string.



Example 10 --Given a string, return a new string where the first and last chars have been exchanged.




Output


1 comment: