Given a string, write a Python program to split the characters of the given string into a list using Python.
Examples:
Input : programmingshark Output : ['p', 'r', 'o', 'g', 'r' 'a', 'm', 'm', 'i', 'n' 'g' 's', 'h', 'a', 'r' 'k'] Input : Word Output : ['W', 'o', 'r', 'd']
The act of unpacking involves taking things out, specifically iterables like dictionaries, lists, and tuples.
string = "programmingshark"
print([*string])
Output:
['p', 'r', 'o', 'g', 'r' 'a', 'm', 'm', 'i', 'n' 'g' 's', 'h', 'a', 'r' 'k']
Here, we are splitting the letters using the native way using the loop and then we are appending it to a new list.
string = 'programmingshark'
lst = []
for letter in string:
lst.append(letter)
print(lst)
Output:
['p', 'r', 'o', 'g', 'r' 'a', 'm', 'm', 'i', 'n' 'g' 's', 'h', 'a', 'r' 'k']
This approach uses list comprehension to convert each character into a list. Using the following syntax you can split the characters of a string into a list.
string = "programmingsharkforprogrammingshark "
letter = [x for x in string]
print(letter)
Output:
['p', 'r', 'o', 'g', 'r' 'a', 'm', 'm', 'i', 'n' 'g' 's', 'h', 'a', 'r' 'k' 'f', 'o', 'r', 'p', 'r', 'o', 'g', 'r' 'a', 'm', 'm', 'i', 'n' 'g' 's', 'h', 'a', 'r' 'k']
Python provides direct typecasting of strings into a list using Python list().
def split(word):
return list(word)
# Driver code
word = 'programmingshark'
print(split(word))
def split(word):
return list(word)
Output:
['p', 'r', 'o', 'g', 'r' 'a', 'm', 'm', 'i', 'n' 'g' 's', 'h', 'a', 'r' 'k']
Extend iterates over its input, expanding the list, and adding each member.
string = 'programmingshark@for'
lst = []
lst.extend(string)
print(lst)
Output:
['p', 'r', 'o', 'g', 'r' 'a', 'm', 'm', 'i', 'n' 'g' 's', 'h', 'a', 'r' 'k' '@', 'f', 'o', 'r']
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.