• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Split a String into Characters in Python

Split a String into Characters in Python

Author: Aditya Raj
Last Updated: December 30, 2022

Strings are used in python to handle text data. In this article, we will discuss different ways to split a string into characters in Python.

Table of Contents
  1. Can We Split a String Into Characters Using the split() Method?
  2. Split a String Into Characters Using the for Loop in Python
  3. String to List of Characters Using List Comprehension
  4. String to List of Characters Using the list() Function in Python
  5. Using the tuple() Function
  6. Conclusion

Can We Split a String Into Characters Using the split() Method?

In python, we usually use the split() method on a string to split it into substrings. The split() method, when invoked on a string, takes a character as a separator as its input argument. After execution, it splits the string at all the instances where the separator is present and returns a list of substrings. For instance, consider the following example.

myStr="Python For Beginners"
print("The input string is:",myStr)
output=myStr.split()
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: ['Python', 'For', 'Beginners']

One can say that we can use an empty string to split a string into characters. Let us try that.

myStr="Python For Beginners"
print("The input string is:",myStr)
output=myStr.split("")
print("The output is:",output)

Output:

ValueError: empty separator

In this example, we have passed an empty string as a separator to split the string into characters. However, the program runs into a ValueError exception saying that you have used an empty separator.

So, we cannot use the split() method to split a python string into characters. Let us discuss other approaches for this.

Split a String Into Characters Using the for Loop in Python

A string in python is an iterable object. Hence, we can access the character of a string one by one using a for loop. 

To split a string using the for loop, we will first define an empty list to contain the output characters. Then, we will iterate through the characters of the string using a python for loop. While iteration, we will add each character of the string to the list using the append() method. The append() method, when invoked on a list, takes a character as its input argument and appends it to the end of the list.

After execution of the for loop, we will get all the characters of the string in the list. You can observe this in the following example.

output=[]
myStr="Python For Beginners"
print("The input string is:",myStr)
for character in myStr:
    output.append(character)
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'F', 'o', 'r', ' ', 'B', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']

In the above example, we have split the string "Python For Beginners" into a list of characters.

String to List of Characters Using List Comprehension

List comprehension is used to create a list from elements of an existing iterable object in python. It’s a better alternative to the approach using a for loop and the append() method as we can convert any iterable object into a list with a single python statement. 

You can split a string into a list of characters using list comprehension as shown below.

myStr="Python For Beginners"
print("The input string is:",myStr)
output=[character for character in myStr]
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'F', 'o', 'r', ' ', 'B', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']

In this example, we have used list comprehension instead of the for loop. Hence, we are able to get the same results in fewer lines of code.

String to List of Characters Using the list() Function in Python

The list() constructor is used to create a list from any iterable object such as a string, set, or tuple in python. It takes the iterable object as its input argument and returns a list containing the elements of the iterable object. 

To break a string into characters, we will pass the input string to the list() function. After execution, it will return a list containing all the characters of the input string. You can observe this in the following example.

myStr="Python For Beginners"
print("The input string is:",myStr)
output=list(myStr)
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'F', 'o', 'r', ' ', 'B', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']

Using the tuple() Function

A tuple is an immutable version of a list. Hence, you can also convert a string into a character of tuples. For this, you can use the tuple() function. The tuple() function takes the iterable object as its input argument and returns a tuple containing the elements of the iterable object. 

To split a string, we will pass the input string to the tuple() function. After execution, it will return a tuple containing all the characters of the input string. You can observe this in the following example.

myStr="Python For Beginners"
print("The input string is:",myStr)
output=tuple(myStr)
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: ('P', 'y', 't', 'h', 'o', 'n', ' ', 'F', 'o', 'r', ' ', 'B', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's')

Conclusion

In this article, we have discussed different ways to split a string into characters in python. 

To learn more about python programming, you can read this article on string manipulation. You might also like this article on python simplehttpserver.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us