user63912
0
Q:

separate a string in python

# Default splits at whitespace
txt = "Welcome to the jungle, baby!"
txt.split()
# Output:
# ["Welcome", "to", "the", "jungle," "baby!"]

# You can specify where to split
txt = "Welcome to the jungle, baby!"
txt.split(", ")
# Output:
# ["Welcome to the jungle", "baby!"]
72
string = "A B C D"
string2 = "E-F-G-H"

# the split() function will return a list
stringlist = string.split() # if you give no arguments, it will separate by whitespaces by default
stringlist2 = string2.split("-", 3) # you can specify the maximum amount of elements the split() function will output
20
file='/home/folder/subfolder/my_file.txt'
file_name=file.split('/')[-1].split('.')[0]
15
str.split(separator, maxsplit)
-2

New to Communities?

Join the community