Use for loop to convert each character into the list and returns the list/array of the characters.
Python program to split string into array of characters using for loop
# Split string using for loop
# function to split string
def split_str(s):
return [ch for ch in s]
# main code
string = "Hello world!"
print("string: ", string)
print("split string...")
print(split_str(string))
2) Split string by converting string to the list (using typecast)
We can typecast string to the list using list(string) – it will return a list/array of characters.
Python program to split string into array by typecasting string to list
# Split string by typecasting
# from string to list
# function to split string
def split_str(s):
return list(s)
# main code
string = "Hello world!"
print("string: ", string)
print("split string...")
print(split_str(string))
Splitting string to characters
1) Split string using for loop
Use for loop to convert each character into the list and returns the list/array of the characters.
Python program to split string into array of characters using for loop
Output
2) Split string by converting string to the list (using typecast)
We can typecast string to the list using list(string) – it will return a list/array of characters.
Python program to split string into array by typecasting string to list
need an explanation for this answer? contact us directly to get an explanation for this answer