Python Tutorial ( PDFDrive )
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']splitlines( num=string.count('\n')) Description The method splitlines() returns a list with all the lines in string, optionally including the line breaks (if num issupplied and is true)Syntax Following is the syntax for splitlines() method:str.splitlines( num=string.count('\n'))Parameters • num -- This is any number, if present then it would be assumed that line breaks need to be included in thelines.Return Value This method returns true if found matching string otherwise false.ExampleThe following example shows the usage of splitlines() method.#!/usr/bin/pythonstr = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d";print str.splitlines( );print str.splitlines( 0 );print str.splitlines( 3 );print str.splitlines( 4 );print str.splitlines( 5 );Let us compile and run the above program, this will produce the following result:['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d']['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d']['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']startswith(str, beg=0,end=len(string)) Description The method startswith() checks whether string starts with str, optionally restricting the matching with the givenindices start and end.Syntax Following is the syntax for startswith() method:str.startswith(str, beg=0,end=len(string));TUTORIALS POINT Simply Easy Learning
Parameters• str -- This is the string to be checked.• beg -- This is the optional parameter to set start index of the matching boundary.• end -- This is the optional parameter to set start index of the matching boundary.Return Value This method returns true if found matching string otherwise false.Example The following example shows the usage of startswith() method.#!/usr/bin/pythonstr = "this is string example....wow!!!";print str.startswith( 'this' );print str.startswith( 'is', 2, 4 );print str.startswith( 'this', 2, 4 );Let us compile and run the above program, this will produce the following result:TrueTrueFalsestrip([chars]) Description The method strip() returns a copy of the string in which all chars have been stripped from the beginning and theend of the string (default whitespace characters).Syntax Following is the syntax for strip() method:str.strip([chars]);Parameters • chars -- The characters to be removed from beginning or end of the string.Return Value This method returns a copy of the string in which all chars have been stripped from the beginning and the end ofthe string.Example The following example shows the usage of strip() method.#!/usr/bin/pythonstr = "0000000this is string example....wow!!!0000000";print str.strip( '0' );Let us compile and run the above program, this will produce the following result:TUTORIALS POINT Simply Easy Learning
- 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 and 134: Description The method expandtabs()
- Page 135 and 136: Traceback (most recent call last):F
- 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: Parameters • chars -- You can sup
- 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
- Page 185 and 186: Syntax Following is the syntax for
- Page 187 and 188: Syntax Following is the syntax for
- Page 189 and 190: Return Value This method does not
- Page 191 and 192: CHAPTER13Python Date & TimeApython
- Page 193 and 194: #!/usr/bin/pythonimport time;localt
- Page 195 and 196: import timeprint "time.altzone %d "
- Page 197 and 198: Parameters • sec -- These are the
- Page 199 and 200: Example The following example shows
Parameters
• str -- This is the string to be checked.
• beg -- This is the optional parameter to set start index of the matching boundary.
• end -- This is the optional parameter to set start index of the matching boundary.
Return Value
This method returns true if found matching string otherwise false.
Example
The following example shows the usage of startswith() method.
#!/usr/bin/python
str = "this is string example....wow!!!";
print str.startswith( 'this' );
print str.startswith( 'is', 2, 4 );
print str.startswith( 'this', 2, 4 );
Let us compile and run the above program, this will produce the following result:
True
True
False
strip([chars])
Description
The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the
end of the string (default whitespace characters).
Syntax
Following is the syntax for strip() method:
str.strip([chars]);
Parameters
• chars -- The characters to be removed from beginning or end of the string.
Return Value
This method returns a copy of the string in which all chars have been stripped from the beginning and the end of
the string.
Example
The following example shows the usage of strip() method.
#!/usr/bin/python
str = "0000000this is string example....wow!!!0000000";
print str.strip( '0' );
Let us compile and run the above program, this will produce the following result:
TUTORIALS POINT
Simply Easy Learning