All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Split String By Character

Last Updated : Mar 11, 2024

Python Split String By Character

In this tutorial we will show you the solution of python split string by character, at times, while working in your programs, you may get a situation where you want to break a string into smaller parts for further processing.

Everything is an object in Python, hence even a string is treated as an object in Python.

A string is a collection of characters that can be symbols, alphabets, numbers, etc.

Step By Step Guide On Python Split String By Character :-

If you have worked on strings in any programming language, then you might know about concatenation (combining the strings). String splitting is just the opposite of it.

In order to perform split operations on strings, Python provides us with a built-in function called list().

demo = "Demo string"
print(list(demo))
my_string1 = "Python"
print(my_string1[0])
print(my_string1[1])
print(my_string1[2])
print(my_string1[3])
print(my_string1[4])
print(my_string1[5])
characters = list(my_string1)
print(characters)
  1. In this approach, we will use the list() function to split a string by characters in a Python program.
  2. You can use the split() function as well to split a string, but it only splits a string by using a separator. A comma in between or a white space between words can be used as a separator.
  3. If you don’t provide any value for the separator, the split() function will, by default, split the string by white spaces.
  4. In the first line, there is a variable named demo that stores the value “Demo string” in it.
  5. In the next line, there is a print statement that displays the values returned from the list() function.
  6. The list() function returns a list as the result. The list() function accepts only one argument, which is a variable name where the string is stored.
  7. In this case, the list() function will return the list of characters generated from the value of the variable “demo”.
  8. In the next line, there is a variable named “my_string1” that has the value of “Python” assigned to it.
  9. You can manually split a string into single characters by accessing the index of that particular character.
  10. In the next few lines, there are some print statements that will display the word “Python” character by character.
  11. This approach doesn’t create a different list for storing the characters split from a string.
  12. In the next line, there is a variable called “characters” that will store the result of the list () function.
  13. In this case, the list function will return the list generated from the value of the variable “my_string1”.
  14. In the last line, there is a print statement that will display the list variable named “characters”.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now split a given string by characters in a Python program.

You can use any of the methods mentioned above that you find easy to understand.

The best way to do the same is to use the first method, which uses the list class and list constructor to split a string by characters.

I hope this tutorial on python split string by character helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪