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
Recommended:-Python Program to Find the GCD of Two Numbers
Recommended:-Python Program to Find the LCM of the Array Elements
Recommended:-Python Code to Draw a Pie Chart
Recommended:-Python Program to Display the Current Date and Time
Recommended:-Python Program to Find Area of Triangle
Recommended:-Python Program to Find the Area of Rectangle
Recommended:-Python Program to Print All Timezones
Recommended:-Python Count the Occurrences in an Array
Recommended:-Python Program to Print Binary Numbers From 1 to N
Recommended:-Python Program to print Elements in a List
Recommended:-Python Program to Swap Characters of Given String