Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active June 30, 2020 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/8f764f22b629f808afac70855a4d929b to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/8f764f22b629f808afac70855a4d929b to your computer and use it in GitHub Desktop.
#Only one occurence of seperator is there.
s1="Python string methods"
print (s1.partition("string"))#Output:('Python ', 'string', ' methods')
print (s1.rpartition("string"))#Output:('Python ', 'string', ' methods')
#more than one cocurence of seperator.
s2="I like to learn Python.Python is a programming language"
print (s2.partition("Python"))#Output:('I like to learn ', 'Python', '.Python is a programming language')
print (s2.rpartition("Python"))#Output:('I like to learn Python.', 'Python', ' is a programming language')
#it is case sensitive. search for the exact seperator.
print (s2.partition("python"))#Output:('I like to learn Python.Python is a programming language', '', '')
#seperator is not found in the string.
s3="Hello world!"
print (s3.partition("hi"))#Output:('Hello world!', '', '')
print (s3.rpartition("hi"))#Output:('', '', 'Hello world!')
#If seperator is not mentioned,raises TypeError
#print (s3.partition())#Output:TypeError: partition() takes exactly one argument (0 given)
#print (s3.rpartition())#Output:TypeError: rpartition() takes exactly one argument (0 given)
s4="12325"
print (s4.partition("2"))#Output:('1', '2', '325')
print (s4.rpartition("2"))#Output:('123', '2', '5')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment