Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active June 29, 2020 06:47
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/73c999a9a465a457d9c9e8163c87c1f4 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/73c999a9a465a457d9c9e8163c87c1f4 to your computer and use it in GitHub Desktop.
#sep and maxlimit not given.By default space is used as seperator.
s1="1 2 3"
print (s1.split())#Output:['1', '2', '3']
print (s1.rsplit())#Output:['1', '2', '3']
print (s1.split(maxsplit=1))#Output:['1', '2 3']
print (s1.rsplit(maxsplit=1))#Output:['1 2', '3']
#only sep is mentioned
s2="1,2,3"
print (s2.split(","))#Output:['1', '2', '3']
print (s2.rsplit(","))#Output:['1', '2', '3']
s3="a,b,,c"
print (s3.split(",",maxsplit=2))#Output:['a', 'b', ',c']
print (s3.rsplit(",",maxsplit=2))#Output:['a,b', '', 'c']
#both sep and maxlimit are mentioned
s4="red,green,blue"
print (s4.split(",",1))#Output:['red', 'green,blue']
print (s4.rsplit(",",1))#Output:['red,green', 'blue']
print (s4.split(",",2))#Output:['red', 'green', 'blue']
print (s4.split(",",3))#Output:['red', 'green', 'blue']
print (s4.rsplit(",",-1))#Output:['red', 'green', 'blue']
print (s4.rsplit(",",maxsplit=3))#Output:['red', 'green', 'blue']
#sep contains more than one character.
s5="1<>2<>3<>4"
print (s5.split())#Output:['1<>2<>3<>4']
print (s5.split(sep="<>",maxsplit=2))#Output:['1', '2', '3<>4']
print (s5.rsplit(sep="<>",maxsplit=2))#Output:['1<>2', '3', '4']
print (s5.split("<>",3))#Output:['1', '2', '3', '4']
s6="helloworld"
print (s6.split('?'))#Output:['helloworld']
s7="Hello World!"
print (s7.split())#Output:['Hello', 'World!']
print (s7.rsplit())#Output:['Hello', 'World!']
s8="a b c d"
print (s8.split())#Output:['a', 'b', 'c', 'd']
print (s8.rsplit())#Output:['a', 'b', 'c', 'd']
print (s8.split(maxsplit=2))#Output:['a', 'b', 'c d']
print (s8.rsplit(maxsplit=2))#Output:['a b', 'c', 'd']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment