Related Articles

Related Articles

Python | Split string into list of characters
  • Difficulty Level : Basic
  • Last Updated : 05 Feb, 2019

Given a string, write a Python program to split the characters of the given string into a list.

Examples:

Input : geeks
Output : ['g', 'e', 'e', 'k', 's']

Input : Word
Output : ['W', 'o', 'r', 'd']

 
Code #1 : Using For loop

This approach uses for loop to convert each character into a list. Just enclosing the For loop within square brackets [] will split the characters of word into list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Split string into characters
def split(word):
    return [char for char in word] 
      
# Driver code
word = 'geeks'
print(split(word))

chevron_right


Output:



['g', 'e', 'e', 'k', 's']

 
Code #2 : Typecasting to list
Python provides direct typecasting of string into list using list().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Split string into characters
def split(word):
    return list(word)
      
# Driver code
word = 'geeks'
print(split(word))

chevron_right


Output:

['g', 'e', 'e', 'k', 's']

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :