Python Split String into Array or List of Characters

Python Split String into Array or List of Characters

Python program split or convert string into array or list of characters; In this tutorial, you will learn how to split string into given array and list of characters in Python.

How to split a string into an array or List of characters python

  • 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 program to split string by space

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

In this python program, you will learn how to use list() function to convert each character into the list and returns the list/array of the characters.

Python program to convert string into 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

Her, you will learn how to use split() function to convert string word into the list and returns the list/array of the characters.

Python program to split string by space

# 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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *