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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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