Friday, January 10, 2014

Basic part-2 | Strings

Hello.... friends, today we are going to discus about the strings in python in this tut we are going to discus how to use strings operations,methods how to use strings in python etc. these are the data types we will study them one by one . So let's start with strings :

String operations : there are various string operations we can use in python as we can concatenating string ,immutable, Repeated sequence in a string , breaking a string in parts, converting integers into strings. Let's see them practically how string operations can be performed


takes an examples of string operations which are very important
concatenating string 
-- S1 + S2


In this example we can see that we are adding two strings or wa are concatenating first_name + last_name here i want to clear out we can't use the space between to words we can use underscore in place of space if we are using them as a variable. as we can see there is no space in between first_name & last_name as we add up both strings as a result we find the result as GovindSingh means to say no space between Govind & Singh. this means space also takes memory in the python. so here we need to provide memory to add the space between Govind & Singh. SO to provide space we add a blank space in between the first_name & last_name.

Repeated sequence in strings
--buffer = "A"*50

now next is as we need to add multiple values to like in buffer overflow we need to repeat the sinle word so we just need to multiply them as

so here we can see if we want to repeat this buffer value then we just need to multiply it.

Slicing-breaking up the string
--string[start:end:steps]

next we want to slicing or we can say breaking a string. Suppose that we want to break the string into part as here we want the particular part of the string so we need to define only the starting point & the ending point. so how we get it :


here we can see i define a string name with value "Govind Singh" & from this value as we need only from 3rd to 8th string value then how we get it here we can see the index value of 3rd word is "i" & 8th value is "i"
Here is so the Slicing value between 3rd to 8th is Gov"ind S"ingh as G is at 0th index & 3rd is "i" so it start from i to 8th value it is "i" before S so slicing from 3:8 is ind S here space is also include as we define above that space also takes memory.

int to string

as in this example is am showing you int is a simple integer then it returns us simple integer value , string as string value . but if in 4th line you can see i define sting value in int so it returns us a string & in the next line vice-verse.
so here we can say that in the numeric type values can be converted into integer to string & vice-verse


String Methods :

python spot various string methods as :

srting.capitalize()
string.decode([encoding])
string.encode([decode])
string.find()
string.replace()
string.split()
string.join()

there are various other methods too but these are important one we are going to study here 
in this example given bellow we define a string Govind & perform the various string methods in it as we want the upper case, lower case, capitalize then we just call the method by just string.method()
in the find method we can see if we want to find the letter not mention in the string then it gives the value -1 this -1 telles us the called letter is not defined i the string value. & the rest we knows the string based on index search the value. as we want to find the govind then this value is here defined at the place of 0th index. to clear it more if we want to find the "ind" value then as we count from the index ind is starting from index value 


to study other string methods let's define a string with name here i want to split the string into parts counter find the space if we are not defining the value . but if we define the value as i defined the value "i" then it check all the i's & split the strings here it remove the i & split the string into parts as shown


same as find method the replace method works as we want to replace the Govind with Amit then it first counter find the govind & then replace the word or string with word or string. all if i want to replace with a then you can see it find all i's & replace them with a's. 

here if you are getting confuse that as we see string name then it is same not be changed then why not it canged from "Govind Singh" to "Amit Singh" because as we discus the string is immutable means the string in the memory will remains the same. So we can say these functions returns the new string rather then modifying the main string 


in the above image we can see how to use encode & decode method. we need to define the string then call the encode & the method as shown in the image. but as we know we need to store this encodes value into memory so that we can decode this string to decode this string again we need to store the decoded string & as we call this value for decode then we get result.

Note: try str.join() method self 

No comments:

Post a Comment