+2

what is split() func in py?

Please describe split() function of Py. I found nothing but some comments in sololearn. Thanks in advance.

3/21/2020 5:49:29 PM

Python piper

5 Answers

New Answer

+14

print("abcd".split("c")) # output ["ab", "d"] print("Hello World!".split()) # output ["Hello", "World!"] The opposite is str.join(), e.g. print(" ".join(["Hello", "World!"])) # output "Hello World!" print("c".join(["ab", "d"])) # output "abcd" You can use these to remove spaces print("".join("abc d".split())) # output "abcd" (edited, thanks to Kevin Star for pointing out my mistake!)

+5

https://code.sololearn.com/c4mJycFIydMq/?ref=app

+4

Split() function takes in a regular expression as an argument and return a list. Without adding an argument, nothing will change in your code. And here's an example that splits "hello world" by blank space print("hello world".split(" "))

+4

David Ashton a lot of thanks

+2

It seperates strings you can use like this: WORD.split( ) default value is " " but you can also write another things like WORD.split("=") or WORLD.split("/") and the output will be a list but there wont be = or / in the list.