how to split a string by character in python

Do you want to know the details regarding “how to split a string by character in python”. If yes, you’re in the correct post.

how to split a string by character in python

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]

Final Thoughts

I hope this article helps you to know about “how to split a string by character in python”. If you have any queries regarding this article please let us know via the comment section. Share this article with your friends & 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