String section () in Python



The partition () method splits the string at the first occurrence of the separator and returns a tuple containing the part of the separator before, the separator and the part after the separator. Here the separator — it is a string that has an argument.

Syntax:
string.partition (separator)

Parameters:
The partition () method takes a separator (a string) as the argument that separates the string at the first occurrence of it.

Returns:
Returns a tuple which contains the part before the separator, separator parameter, and the part after the separator if the separator parameter is found in the string.
Returns a tuple which contains string itself and two empty strings if the separator parameter is not found.

CODE 1

string = "pawan is a good"

 
# & # 39; is & # 39; delimiter found

print (string.partition ( `is` ))

 
Separator # not found

print (string.partition ( `bad` ))

 

string = "pawan is a good, isn`t it"

 
# splits when & # 39; is & # 39;

print (string.partition ( `is ` ))

Output:

 (`pawan`, `is`, `a good`) (` pawan is a good`, ` `,` `) (` pawan `,` is`, "a good, isn`t it") 

Codex2

string = "geeks is a good"

 
# & # 39; is & # 39; delimiter found

print (string.partition ( `is` ))

 
Separator # not found

print (string.partition ( `bad` ))

 

string = "geeks is a good, isn`t it"

 
# splits when & # 39; is & # 39;

print (string.partition ( `is ` ))

Output:

 (`geeks`, `is`, `a good`) (` geeks is a good`, ` `,` `) (` geeks `,` is`, "a good, isn`t it")