Python Split String into Array of Characters

Python split string into array of characters; Through this tutorial, i will show you how to split string into array of characters or list of characters in python.

Python Split String into Array of Characters

Now, i am going to show you simple 3 ways to split string into array of characters and list of characters using for loop, list function; as shown below:

  • Python program to split the string into an array of characters using for loop
  • Python Split string by converting string to the list Using list() function
  • Python split string by space delimiter

Python program to split the string into an array of characters using for loop

  • Define a function, which is used to convert string into array using for loop.
  • Take input string from user by using input() function.
  • Call function and print result.
# Split string using for loop
# function to split string
def split_str(s):
  return [ch for ch in s]
# take string from user
str1 = input("Please Enter String : ")
print("string: ", str1)
print("split string...")
print(split_str(str1))

After executing the program, the output will be:

Please Enter String :  developer world
string:  developer world
split string...
['d', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', ' ', 'w', 'o', 'r', 'l', 'd']

Python Split string by converting string to the list Using list() function

  • Define a function, which is used to convert string into array using list() function.
  • Take input string from user by using input() function.
  • Call function and print result.
# Split string using list function
# function to split string
def split_str(s):
  return list(s)
# take string from user
str1 = input("Please Enter String : ")
print("string: ", str1)
print("split string...")
print(split_str(str1))

After executing the program, the output will be:

Please Enter String :  my world
string:  my world
split string...
['m', 'y', ' ', 'w', 'o', 'r', 'l', 'd']

Python split string by space delimiter

# Split string using split function
txt = "hello world, i am developer"
x = txt.split(" ")
print(x)

After executing the program, the output will be:

[‘hello’, ‘world,’, ‘i’, ‘am’, ‘developer’]

Recommended Python Tutorials

Leave a Comment