Python Tutorial ( PDFDrive )

28.10.2021 Views

['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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!