91-9990449935 0120-4256464 |
PYTHON STRINGSStrings are the simplest and easy to use in Python. String pythons are immutable. We can simply create Python String by enclosing a text in single as well as double quotes. Python treat both single and double quotes statements same. Accessing Strings:
eg: str[0]='P'=str[-6] , str[1]='Y' = str[-5] , str[2] = 'T' = str[-4] , str[3] = 'H' = str[-3] str[4] = 'O' = str[-2] , str[5] = 'N' = str[-1]. Simple program to retrieve String in reverse as well as normal form. name="Rajat" length=len(name) i=0 for n in range(-1,(-length-1),-1): print name[i],"\t",name[n] i+=1 Output: >>> R t a a j j a a t R >>> Strings OperatorsThere are basically 3 types of Operators supported by String:
Basic Operators:There are two types of basic operators in String. They are "+" and "*". String Concatenation Operator :(+)The concatenation operator (+) concatenate two Strings and forms a new String. eg: >>> "ratan" + "jaiswal" Output: 'ratanjaiswal' >>>
NOTE: Both the operands passed for concatenation must be of same type, else it will show an error. Eg: 'abc' + 3 >>> output: Traceback (most recent call last): File " Replication Operator: (*)Replication operator uses two parameter for operation. One is the integer value and the other one is the String. The Replication operator is used to repeat a string number of times. The string will be repeated the number of times which is given by the integer value. Eg: >>> 5*"Vimal" Output: 'VimalVimalVimalVimalVimal'
NOTE: We can use Replication operator in any way i.e., int * string or string * int. Both the parameters passed cannot be of same type. Membership OperatorsMembership Operators are already discussed in the Operators section. Let see with context of String. There are two types of Membership operators: 1) in:"in" operator return true if a character or the entire substring is present in the specified string, otherwise false. 2) not in:"not in" operator return true if a character or entire substring does not exist in the specified string, otherwise false. Eg: >>> str1="trainingtrains" >>> str2='sssit' >>> str3="seomount" >>> str4='java' >>> st5="it" >>> str6="seo" >>> str4 in str1 True >>> str5 in str2 >>> st5 in str2 True >>> str6 in str3 True >>> str4 not in str1 False >>> str1 not in str4 True Relational Operators:All the comparison operators i.e., (<,><=,>=,==,!=,<>) are also applicable to strings. The Strings are compared based on the ASCII value or Unicode(i.e., dictionary Order). Eg: >>> "RAJAT"=="RAJAT" True >>> "afsha">='Afsha' True >>> "Z"<>"z" True Explanation: The ASCII value of a is 97, b is 98, c is 99 and so on. The ASCII value of A is 65,B is 66,C is 67 and so on. The comparison between strings are done on the basis on ASCII value. Slice Notation:String slice can be defined as substring which is the part of string. Therefore further substring can be obtained from a string. There can be many forms to slice a string. As string can be accessed or indexed from both the direction and hence string can also be sliced from both the direction that is left and right. Syntax: <string_name>[startIndex:endIndex], <string_name>[:endIndex], <string_name>[startIndex:] Example: >>> str="Nikhil" >>> str[0:6] 'Nikhil' >>> str[0:3] 'Nik' >>> str[2:5] 'khi' >>> str[:6] 'Nikhil' >>> str[3:] 'hil' Note: startIndex in String slice is inclusive whereas endIndex is exclusive. String slice can also be used with Concatenation operator to get whole string. Eg: >>> str="Mahesh" >>> str[:6]+str[6:] 'Mahesh' //here 6 is the length of the string. String Functions and Methods:There are many predefined or built in functions in String. They are as follows:
Examples: 1) capitalize() >>> 'abc'.capitalize() Output: 'Abc' 2) count(string) msg = "welcome to sssit"; substr1 = "o"; print msg.count(substr1, 4, 16) substr2 = "t"; print msg.count(substr2) Output: >>> 2 2 >>> 3) endswith(string) string1="Welcome to SSSIT"; substring1="SSSIT"; substring2="to"; substring3="of"; print string1.endswith(substring1); print string1.endswith(substring2,2,16); print string1.endswith(substring3,2,19); print string1.endswith(substring3); Output: >>> True False False False >>> 4) find(string) str="Welcome to SSSIT"; substr1="come"; substr2="to"; print str.find(substr1); print str.find(substr2); print str.find(substr1,3,10); print str.find(substr2,19); Output: >>> 3 8 3 -1 >>> 5) index(string) str="Welcome to world of SSSIT"; substr1="come"; substr2="of"; print str.index(substr1); print str.index(substr2); print str.index(substr1,3,10); print str.index(substr2,19); Output: >>> 3 17 3 Traceback (most recent call last): File "C:/Python27/fin.py", line 7, in 6) isalnum() str="Welcome to sssit"; print str.isalnum(); str1="Python47"; print str1.isalnum(); Output: >>> False True >>> 7) isalpha() string1="HelloPython"; # Even space is not allowed print string1.isalpha(); string2="This is Python2.7.4" print string2.isalpha(); Output: >>> True False >>> 8) isdigit() string1="HelloPython"; print string1.isdigit(); string2="98564738" print string2.isdigit(); Output: >>> False True >>> 9) islower() string1="Hello Python"; print string1.islower(); string2="welcome to " print string2.islower(); Output: >>> False True >>> 10) isupper() string1="Hello Python"; print string1.isupper(); string2="WELCOME TO" print string2.isupper(); Output: >>> False True >>> 11) isspace() string1=" "; print string1.isspace(); string2="WELCOME TO WORLD OF PYT" print string2.isspace(); Output: >>> True False >>> 12) len(string) string1=" "; print len(string1); string2="WELCOME TO SSSIT" print len(string2); Output: >>> 4 16 >>> 13) lower() string1="Hello Python"; print string1.lower(); string2="WELCOME TO SSSIT" print string2.lower(); Output: >>> hello python welcome to sssit >>> 14) upper() string1="Hello Python"; print string1.upper(); string2="welcome to SSSIT" print string2.upper(); Output: >>> HELLO PYTHON WELCOME TO SSSIT >>> 15) startswith(string) string1="Hello Python"; print string1.startswith('Hello'); string2="welcome to SSSIT" print string2.startswith('come',3,7); Output: >>> True True >>> 16) swapcase() string1="Hello Python"; print string1.swapcase(); string2="welcome to SSSIT" print string2.swapcase(); Output: >>> hELLO pYTHON WELCOME TO sssit >>> 17) lstrip() string1=" Hello Python"; print string1.lstrip(); string2="@@@@@@@@welcome to SSSIT" print string2.lstrip('@'); Output: >>> Hello Python welcome to world to SSSIT >>> 18) rstrip() string1=" Hello Python "; print string1.rstrip(); string2="@welcome to SSSIT!!!" print string2.rstrip('!'); Output: >>> Hello Python @welcome to SSSIT >>>
Next TopicPython Lists
|