Python Tutorial ( PDFDrive )
Example The following example shows the usage of find() method.#!/usr/bin/pythonstr1 = "this is string example....wow!!!";str2 = "exam";print str1.find(str2);print str1.find(str2, 10);print str1.find(str2, 40);Let us compile and run the above program, this will produce the following result:1515-1index(str, beg=0, end=len(string)) Description The method index() determines if string str occurs in string or in a substring of string if starting indexbeg andending index end are given. This method is same as find(), but raises an exception if sub is not found.Syntax Following is the syntax for index() method:str.index(str, beg=0 end=len(string))Parameters • str -- This specifies the string to be searched.• beg -- This is the starting index, by default its 0.• end -- This is the ending index, by default its equal to the length of the string.Return Value This method returns index if found otherwise raises an exception if str is not found.Example The following example shows the usage of index() method.#!/usr/bin/pythonstr1 = "this is string example....wow!!!";str2 = "exam";print str1.index(str2);print str1.index(str2, 10);print str1.index(str2, 40);Let us compile and run the above program, this will produce the following result:1515TUTORIALS POINT Simply Easy Learning
Traceback (most recent call last):File "test.py", line 8, inprint str1.index(str2, 40);ValueError: substring not foundshell returned 1Note: We would see how to handle exceptions in subsequent chapters. So for the time being leave it as it is.isalnum() Description The method isalnum() checks whether the string consists of alphanumeric characters.Syntax Following is the syntax for isalnum() method:str.isa1num()Parameters • NAReturn Value This method returns true if all characters in the string are alphanumeric and there is at least one character, falseotherwise.Example The following example shows the usage of isalnum() method.#!/usr/bin/pythonstr = "this2009"; # No space in this stringprint str.isalnum();str = "this is string example....wow!!!";print str.isalnum();Let us compile and run the above program, this will produce the following result:TrueFalseisalpha() Description The method isalpha() checks whether the string consists of alphabetic characters only.Syntax Following is the syntax for islpha() method:str.isalpha()TUTORIALS POINT Simply Easy Learning
- Page 83 and 84: Loop Typewhile loopfor loopDescript
- Page 85 and 86: num = raw_input("Enter a number :")
- Page 87 and 88: Good bye!Iterating by Sequence I
- Page 89 and 90: 11 is prime13 is prime17 is prime19
- Page 91 and 92: continue statement The continue st
- Page 93 and 94: CHAPTER8Python NumbersNumber data t
- Page 95 and 96: max(x1, x2,...)min(x1, x2,...)modf(
- Page 97 and 98: Syntax Following is the syntax for
- Page 99 and 100: import math# This will import math
- Page 101 and 102: import mathmath.log10( x )Note: Thi
- Page 103 and 104: modf(x) Description The method modf
- Page 105 and 106: round( x [, n] )Parameters • x --
- Page 107 and 108: Return Value This method returns a
- Page 109 and 110: seed([x]) Description The method se
- Page 111 and 112: Syntax Following is the syntax for
- Page 113 and 114: asin(x)Note: This function is not a
- Page 115 and 116: print "atan2(-10,10) : ", math.atan
- Page 117 and 118: Note: This function is not accessib
- Page 119 and 120: import mathprint "degrees(3) : ", m
- Page 121 and 122: CHAPTER9Python StringsStrings are a
- Page 123 and 124: + Concatenation - Adds values on ei
- Page 125 and 126: TAB ( \t ) and they will show up th
- Page 127 and 128: 14151617181920212223242526272829303
- Page 129 and 130: str = "this is string example....wo
- Page 131 and 132: Return Value This method returns a
- Page 133: Description The method expandtabs()
- Page 137 and 138: Let us compile and run the above pr
- Page 139 and 140: str = "This is string example....wo
- Page 141 and 142: str = "-";seq = ("a", "b", "c"); #
- Page 143 and 144: lstrip() Description The method lst
- Page 145 and 146: Max character: xmin(str) Descriptio
- Page 147 and 148: str = "this is really a string exam
- Page 149 and 150: Parameters • chars -- You can sup
- Page 151 and 152: Parameters• str -- This is the st
- Page 153 and 154: Parameters • NAReturn Value This
- Page 155 and 156: Example The following example shows
- Page 157 and 158: CHAPTER10Python ListsThe most basic
- Page 159 and 160: Python Expression Results Descripti
- Page 161 and 162: Syntax Following is the syntax for
- Page 163 and 164: list( seq )Parameters • seq -- Th
- Page 165 and 166: Return Value If elements are of th
- Page 167 and 168: list.insert(index, obj)Parameters
- Page 169 and 170: Syntax Following is the syntax for
- Page 171 and 172: CHAPTER11Python TuplesAtuple is a s
- Page 173 and 174: len((1, 2, 3)) 3 Length(1, 2, 3) +
- Page 175 and 176: -11-1len(tuple) Description The met
- Page 177 and 178: min value element : 200tuple(seq) D
- Page 179 and 180: #!/usr/bin/pythondict = {'Name': 'Z
- Page 181 and 182: 34str(dict)Produces a printable str
- Page 183 and 184: type(variable) Description The meth
Example
The following example shows the usage of find() method.
#!/usr/bin/python
str1 = "this is string example....wow!!!";
str2 = "exam";
print str1.find(str2);
print str1.find(str2, 10);
print str1.find(str2, 40);
Let us compile and run the above program, this will produce the following result:
15
15
-1
index(str, beg=0, end=len(string))
Description
The method index() determines if string str occurs in string or in a substring of string if starting indexbeg and
ending index end are given. This method is same as find(), but raises an exception if sub is not found.
Syntax
Following is the syntax for index() method:
str.index(str, beg=0 end=len(string))
Parameters
• str -- This specifies the string to be searched.
• beg -- This is the starting index, by default its 0.
• end -- This is the ending index, by default its equal to the length of the string.
Return Value
This method returns index if found otherwise raises an exception if str is not found.
Example
The following example shows the usage of index() method.
#!/usr/bin/python
str1 = "this is string example....wow!!!";
str2 = "exam";
print str1.index(str2);
print str1.index(str2, 10);
print str1.index(str2, 40);
Let us compile and run the above program, this will produce the following result:
15
15
TUTORIALS POINT
Simply Easy Learning