Python split string by character | Example code

Using List Comprehension or Typecasting to list will split string by character in Python. Both methods will convert string into a char of list.

Simple take the string and pass it to list().

Note: You can’t use the split method for it, because this method needed a separator.

Python split string by character example

Simple example code.

Using List Comprehension

s = "Hello"
res = [char for char in s]

print(res)

Typecasting to list 

Using list function to direct typecasting of string into a list.

s = "Hello"
res = list(s)

print(res)

Output:

Python split string by character

Using for loop

This method will print each char, not as a list.

s = "Hello"
for c in s:
    print(c)

Output:

H
e
l
l
o

Do comment if you have any doubts and suggestions on this Python split topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.