split string in python

Do you want to know the information about “split string in python”. If yes, you’re in the correct tutorial.

split string in python

spam = "A B C D"
eggs = "E-F-G-H"

# the split() function will return a list
spam_list = spam.split()
# if you give no arguments, it will separate by whitespaces by default
# ["A", "B", "C", "D"]

eggs_list = eggs.split("-", 3)
# you can specify the maximum amount of elements the split() function will output
# ["E", "F", "G"]
def split(word): 
    return [char for char in word]  
      
# Driver code 
word = 'geeks'
print(split(word)) 

#Output ['g', 'e', 'e', 'k', 's']
string = 'James Smith Bond'
x = string.split(' ') #Splits every ' ' (space) in the string to a list
# x = ['James','Smith','Bond']
print('The name is',x[-1],',',x[0],x[-1])
file="/home/folder/subfolder/my_file.txt"
file_name=file.split('/')[-1].split('.')[0]
# string арга .split() нь мөрийг зүйлийн жагсаалтад хуваадаг: 
# -> Хэрэв аргумент дамжаагүй бол үндсэн үйлдэл нь хоосон зайд хуваагдах явдал юм. 
# -> Хэрэв аргументыг арга руу дамжуулсан бол уг утгыг мөрийг хуваах хязгаарлагч болгон ашиглана.

text = "Silicon Valley"
 
print(text.split())     
# Prints: ['Silicon', 'Valley']
 
print(text.split('i'))  
# Prints: ['S', 'l', 'con Valley']

Final Thoughts

I hope this post helps you to know about “split string in python”. If you have any doubts regarding this tutorial please let us know via the comment section. Share this article with your friends and family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment